用于gmail邮件发送的asp.net代码

时间:2017-05-23 05:48:24

标签: c# asp.net asp.net-mvc gmail mongodb-.net-driver

通过Gmail发送邮件的网络代码无法正常工作,例如安全性较低的应用开启,强大的密码,有无应用密码,有无两步验证。

编码: -

{
    var fromAddress = new MailAddress("doctor@gmail.com");    
    var fromPassword = "idntcare";        
    var toAddress = new MailAddress("thevarghese@gmail.com");    
    string subject = "subject";    
    string body = "body";    
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
    {
         Host = "smtp.gmail.com",
         Port = 587,
         EnableSsl = true,
         DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
         UseDefaultCredentials = false,
         Credentials = new NetworkCredential(fromAddress.Address, fromPassword)

    };

    using (var message = new MailMessage(fromAddress, toAddress)
    {
         Subject = subject,
         Body = body
    })    
    smtp.Send(message);    
}

3 个答案:

答案 0 :(得分:1)

尝试这样做

using (var message = new MailMessage(fromAddress, toAddress)
{
     Subject = subject,
     Body = body
     smtp.Send(message);
})    

或试试这个

using (MailMessage mm = new MailMessage(from, emailTo))
{
    var mailAddress = new MailAddress("address", "name for id");
    mm.From = mailAddress;
    mm.Subject = title;
    mm.Body = HttpUtility.HtmlDecode(body);
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential(from, password);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = NetworkCred;
    smtp.Send(mm);
}

答案 1 :(得分:0)

试试这个。

MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress("doctor@gmail.com");
// Recipient e-mail address.
Msg.To.Add("thevarghese@gmail.com");
Msg.Subject ="subject";
Msg.Body = "body";
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential("user name","password");
smtp.EnableSsl = true;
smtp.Send(Msg);

答案 2 :(得分:0)

{{1}}

尝试使用这些,您可以使用端口号587或25。