我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
我想用我的Gmail帐户发送电子邮件,而不是依靠我的主机发送电子邮件。这些邮件是发给我在节目中演出的乐队的个性化邮件。
有可能做到吗?
当前回答
来源:在ASP发送电子邮件。净c#
下面是一个使用c#发送邮件的示例工作代码,在下面的例子中,我使用谷歌的smtp服务器。
代码是相当不言自明的,替换电子邮件和密码与您的电子邮件和密码值。
public void SendEmail(string address, string subject, string message)
{
string email = "yrshaikh.mail@gmail.com";
string password = "put-your-GMAIL-password-here";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
其他回答
我也遇到了同样的问题,但通过gmail的安全设置和允许不太安全的应用程序解决了。 Domenic & Donny的代码有效,但前提是你启用了该设置
如果您已登录(到谷歌),您可以按照此链接并切换“打开”以“访问不太安全的应用程序”
包括这个,
using System.Net.Mail;
然后,
MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("mail-id@gmail.com","password");
client.EnableSsl = true;
client.Send(sendmsg);
using System;
using System.Net;
using System.Net.Mail;
namespace SendMailViaGmail
{
class Program
{
static void Main(string[] args)
{
//Specify senders gmail address
string SendersAddress = "Sendersaddress@gmail.com";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "ReceiversAddress@yahoo.com";
//Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
const string SendersPassword = "Password";
//Write the subject of ur mail
const string subject = "Testing";
//Write the contents of your mail
const string body = "Hi This Is my Mail From Gmail";
try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
编辑2022 从2022年5月30日起,谷歌将不再支持仅使用用户名和密码登录谷歌账户的第三方应用程序或设备。 但是你仍然可以通过你的gmail账户发送电子邮件。
访问https://myaccount.google.com/security,打开两步验证。如有需要,请电话确认。 点击“应用程序密码”,就在“2步验证”勾的下方。 请求邮件应用程序的新密码。
现在只需使用这个密码,而不是原来的一个为您的帐户!
public static void SendMail2Step(string SMTPServer, int SMTP_Port, string From, string Password, string To, string Subject, string Body, string[] FileNames) {
var smtpClient = new SmtpClient(SMTPServer, SMTP_Port) {
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = true
};
smtpClient.Credentials = new NetworkCredential(From, Password); //Use the new password, generated from google!
var message = new System.Net.Mail.MailMessage(new System.Net.Mail.MailAddress(From, "SendMail2Step"), new System.Net.Mail.MailAddress(To, To));
smtpClient.Send(message);
}
像这样使用:
SendMail2Step("smtp.gmail.com", 587, "youraccount@gmail.com",
"yjkjcipfdfkytgqv",//This will be generated by google, copy it here.
"recipient@barcodes.bg", "test message subject", "Test message body ...", null);
对于其他答案工作“从服务器”首先打开访问gmail帐户不太安全的应用程序。这将在2022年5月30日被弃用
看来最近谷歌改变了安全策略。评分最高的答案不再有效,直到您更改您的帐户设置如下所述:https://support.google.com/accounts/answer/6010255?hl=en-GB 2016年3月,谷歌再次更改设置地点!
这是我的版本:“发送电子邮件在c#使用Gmail”。
using System;
using System.Net;
using System.Net.Mail;
namespace SendMailViaGmail
{
class Program
{
static void Main(string[] args)
{
//Specify senders gmail address
string SendersAddress = "Sendersaddress@gmail.com";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "ReceiversAddress@yahoo.com";
//Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
const string SendersPassword = "Password";
//Write the subject of ur mail
const string subject = "Testing";
//Write the contents of your mail
const string body = "Hi This Is my Mail From Gmail";
try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}