关闭

使用asp.net通过gmail发送电子邮件

标签: asp.netgmailauthenticationsslserverdomain
1433人阅读 评论(0) 收藏 举报

这里是一种使用asp.net通过gmail发送电子邮件,求使用Asp.net2.0中的System.Net.Mail类库实现此功能的代码(我写了个不知为什么就是不能发)

   System.Web.Mail.MailMessage MyMail = new System.Web.Mail.MailMessage();
        //1 代表使用 local smtp, 2 為外部 smtp
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
        //SMTP Server domain name
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
        //Server port, gmail use ssl smtp authentication, port number is 465
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);
        //Authentication method, ssl or not, Username and password for the SMTP Server
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
        //cdoBasic 基本驗證
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
        //帳號名稱 (Demo自行填入)
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusername", "XX@gmail.com");
        //帳號密碼 (Demo自行填入)
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendpassword", "pwd");
        MyMail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendemailaddress", "XX@XX.XX");
        MyMail.From = "
XX@XX.XX";
        MyMail.To = "
XX@XX.XX";
        MyMail.Subject = "ss";
        MyMail.BodyFormat = System.Web.Mail.MailFormat.Text;
        MyMail.Body = "ssss";
        //SmtpMail.SmtpServer="smtp.gmail.com"; //指定SMTP另一寫法
        System.Web.Mail.SmtpMail.Send(MyMail);

 

以下是Asp.net2.0写得,大家看一下有什么错误吗?

        using System.Net.Mail;      

        MailAddress mAdr = new MailAddress("XX@XX.XX", "XXX", System.Text.Encoding.UTF8);
        MailMessage mail = new MailMessage();
        //mail.ReplyTo = mAdr;
        //mail.Sender = mAdr;
        mail.From = mAdr;
        mail.To.Add("
XX@XX.XX");
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Subject = "邮件主题";
        mail.IsBodyHtml = true;
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.Body = "邮件正文";
        mail.Priority = MailPriority.Normal;
        SmtpClient smtp = new SmtpClient();
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 465;
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("
XX@XX.XX", "pwd");       
        smtp.Timeout = 10;
        smtp.Send(mail);

========================

近日看到一篇文章:  http://www.codeproject.com/useritems/SendMailUsingGmailAccount.asp?print=true

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:103325次
    • 积分:1564
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:36篇
    • 译文:0篇
    • 评论:13条