开发者社区> 问答> 正文

C#调用邮箱smtp

请问自己申请的阿里邮smtp_箱能调用SMTP发送邮件嘛还是要购买

展开
收起
james_harden 2016-03-02 10:01:20 4073 0
3 条回答
写回答
取消 提交回答
  • static string accountName = System.Configuration.ConfigurationManager.AppSettings["mailAccountName"];

        static string password = System.Configuration.ConfigurationManager.AppSettings["mailPassword"];
        static string smtpServer = System.Configuration.ConfigurationManager.AppSettings["mailSmtpServer"];
        static int smtpPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["mailSmtpPort"]);
        static string displayName= System.Configuration.ConfigurationManager.AppSettings["mailDisplayName"];
        static string userState= System.Configuration.ConfigurationManager.AppSettings["mailUserState"];
    
        static public void SendMail(string sendTo, string subject, string body)
        {
            try
            {
                // Command line argument must the the SMTP host.
                SmtpClient client = new SmtpClient(smtpServer, smtpPort);
                client.UseDefaultCredentials = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Credentials = new NetworkCredential(accountName, password);
                
                // Specify the e-mail sender.
                // Create a mailing address that includes a UTF8 character
                // in the display name.
                MailAddress from = new MailAddress(accountName,
                   displayName,
                System.Text.Encoding.UTF8);
                // Set destinations for the e-mail message.
                MailAddress to = new MailAddress(sendTo);
                // Specify the message content.
                MailMessage message = new MailMessage(from, to);
                message.Body = body;
                // Include some non-ASCII characters in body and subject.
                string someArrows = "(请勿回复这封邮件)";
                message.Body += Environment.NewLine + someArrows;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = subject;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                client.Send(message);
    
                message.Dispose();
                client.Dispose();
            }
            catch { }
        }      
    2019-07-17 18:32:02
    赞同 展开评论 打赏
  • 还是不行,现在我换了阿里API接口好了

    2019-07-17 18:32:02
    赞同 展开评论 打赏
  • 旺旺:nectar2。

    您好,

    里边的:

    new SmtpClient("smtpdm.aliyun.com", 25);

    是否应该为:

    new SmtpClient("smtp.dm.aliyun.com", 25);

    2019-07-17 18:32:01
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载