通过 gmail 使用 System.Net.Mail 发送电子邮件 [英] Send email using System.Net.Mail through gmail

查看:26
本文介绍了通过 gmail 使用 System.Net.Mail 发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 gmail 服务器发送电子邮件.我已经输入了以下代码,但在发送时卡住了.任何想法请....

I want to send a email through gmail server. I have put the following code but it is getting stuck while sending. Any idea please....

MailMessage mail = new MailMessage();

        mail.From = new System.Net.Mail.MailAddress("apps@xxxx.com");

        //create instance of smtpclient
        SmtpClient smtp = new SmtpClient();
        smtp.Port = 465;
        smtp.UseDefaultCredentials = true;

        smtp.Host = "smtp.gmail.com";            

        smtp.EnableSsl = true;

        //recipient address
        mail.To.Add(new MailAddress("yyyy@xxxx.com"));

        //Formatted mail body
        mail.IsBodyHtml = true;
        string st = "Test";

        mail.Body = st;
        smtp.Send(mail);

xxxx.com 是 Google 应用中的邮件域.谢谢...

The xxxx.com is a mail domain in Google apps. Thanks...

推荐答案

MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("apps@xxxx.com");

// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;   // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential(mail.From,  "password_here");  // [4] Added this. Note, first parameter is NOT string.
smtp.Host = "smtp.gmail.com";            

//recipient address
mail.To.Add(new MailAddress("yyyy@xxxx.com"));

//Formatted mail body
mail.IsBodyHtml = true;
string st = "Test";

mail.Body = st;
smtp.Send(mail);

这篇关于通过 gmail 使用 System.Net.Mail 发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆