use*_*510 4 c# asp.net gmail smtp gmail-imap
我们可以使用gmail smtp从本地主机发送电子邮件吗?我正在尝试并收到错误操作已超时.
我想在过去3天内从本地主机发送电子邮件.如果我使用gmail从我的托管服务器发送电子邮件但它不能在localhost上工作,它工作正常.我已经禁用了防火墙防病毒,但即便如此也是不吉利的.请指导我你曾经使用过gmail从localhost发送电子邮件(不涉及任何服务器)
如果有可能,我的代码请指导我.请帮帮我,引导我,我被困了.
谢谢
protected void btnConfirm_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
message.To.Add("me@hotmail.com");
message.From = new MailAddress("xxxxxx@gmail.com");
message.Subject = "New test mail";
message.Body = "Hello test message succeed";
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.ASCII;
message.Priority = System.Net.Mail.MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Port = 465;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new NetworkCredential("xxxxxx@gmail.com", "**mypassword**");
try
{
smtp.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
是的,您可以使用localhost中的gmail发送电子邮件.
粘贴我博客帖子中的代码片段.
这是工作代码,我经常使用它.
/// <summary>
/// A Generic Method to send email using Gmail
/// </summary>
/// <param name="to">The To address to send the email to</param>
/// <param name="subject">The Subject of email</param>
/// <param name="body">The Body of email</param>
/// <param name="isBodyHtml">Tell whether body of email will be html of plain text</param>
/// <param name="mailPriority">Set the mail priority to low, medium or high</param>
/// <returns>Returns true if email is sent successfuly</returns>
public static Boolean SendMail(String to, String subject, String body, Boolean isBodyHtml, MailPriority mailPriority)
{
try
{
// Configure mail client (may need additional
// code for authenticated SMTP servers)
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
// set the network credentials
mailClient.Credentials = new NetworkCredential("YourGmailEmail@gmail.com", "YourGmailPassword");
//enable ssl
mailClient.EnableSsl = true;
// Create the mail message (from, to, subject, body)
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("YourGmailEmail@gmail.com");
mailMessage.To.Add(to);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
mailMessage.Priority = mailPriority;
// send the mail
mailClient.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5384 次 |
| 最近记录: |