我正在尝试使用gmail stmp服务器通过C#向我自己发送电子邮件,但我收到了来自gmail团队的电子邮件,说“Google刚刚屏蔽了一个不太安全的应用访问您的Google帐户。”。现在我已经更改了设置以允许不太安全的应用程序登录谷歌,但我无法向自己发送电子邮件。这是我的代码。
private static string sendMail(System.Net.Mail.MailMessage mm)
{
try
{
string smtpHost = "smtp.gmail.com";
string userName = "myemail@gmail.com";//write your email address
string password = "xxxxxx";//write password
System.Net.Mail.SmtpClient mClient = new System.Net.Mail.SmtpClient();
mClient.Port = 587;
mClient.EnableSsl = true;
mClient.UseDefaultCredentials = false;
mClient.Credentials = new NetworkCredential(userName, password);
mClient.Host = smtpHost;
mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mClient.Send(mm);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return "Send Sucessfully";
}
private void f()
{
i = i + 1;
string sysName = string.Empty;
string sysUser = string.Empty;
System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress("myemail@gmail.com");
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("myemail@gmail.com");
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(fromAddress, toAddress);
sysName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
sysUser = System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
mm.Subject = sysName + " " + sysUser;
string filename = string.Empty;
mm.IsBodyHtml = true;
mm.BodyEncoding = System.Text.Encoding.UTF8;
MessageBox.Show(sendMail(mm).ToString());
//sendMail(mm);
}
private void Form1_Load(object sender, EventArgs e)
{
f();
}
我也尝试过我的雅虎帐户。然后我也没有收到任何电子邮件。 我已将端口更改为465并将smtphost更改为“mail.yahoo.com”,并将电子邮件地址更改为mymail@yahoo.com
答案 0 :(得分:0)
protected void SendMail()
{
// Gmail Address from where you send the mail
var fromAddress = "Gmail@gmail.com";
// any address where the email will be sending
var toAddress = YourEmail.Text.ToString();
//Password of your gmail address
const string fromPassword = "Password";
// Passing the values and make a email formate to display
string subject = YourSubject.Text.ToString();
string body = "From: " + YourName.Text + "\n";
body += "Email: " + YourEmail.Text + "\n";
body += "Subject: " + YourSubject.Text + "\n";
body += "Question: \n" + Comments.Text + "\n";
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
您已使用此代码。 它正在运行代码。
答案 1 :(得分:-1)
转到Google帐户设置,并允许安全性较低的应用程序。 https://myaccount.google.com/u/1/security?utm_source=OGB#signin 在底部"允许不太安全的应用程序"滑块是您需要使用的滑块。
默认的Google SMTP设置为here。