zoukankan      html  css  js  c++  java
  • C# 发送邮件整理,包括控制台程序、WPF、WebForm 及 ASP.NET MVC

      发送邮件使用SMTP服务器,有两种方案,一种是使用IIS的SMTP功能;另一种是直接使用邮件供应商的SMTP,比如Gmail、Sina、QQ等,使用这些SMTP服务器必须得注册帐号,一般可以直接用邮箱及密码,但是有些邮箱必须开启POP3/SMTP服务才可以,比如QQ邮箱默认是关闭的,可以在“设置”->“账户”里面找到。我今天整理的都是用的第二种。

      早期的.NET版本用的是 System.Web.Mail 类提供的功能来发邮件;2.0版本推出了 System.Net.Mail 类来代替 System.Web.Mail ,但是我在 WebForm 里面使用的时候用System.Net.Mail 老是触发异常,后来改用 System.Web.Mail 才成功了,可以看下我代码里的区别;ASP.NET MVC 3 提供了 WebMail 类来发送邮件,更加方便。

      控制台程序、WPF、WebForm 及 ASP.NET MVC 我都测试了一下,控制台程序和 WPF 都用了 System.Net.Mail ,WebForm 和 ASP.NET MVC 都可以用 System.Web.Mail ,而 ASP.NET MVC 3 直接用 WebMail 更方便。下面我把代码分别贴出来。

      控制台程序:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Net.Mail;//添加引用
     6   
     7 namespace SendEmail
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Program p = new Program();
    14             bool flag = p.SendEmail();
    15             if (flag)
    16             {
    17                 Console.Write("Success !");
    18             }
    19               
    20             Console.Read();
    21         }
    22   
    23         public bool SendEmail()
    24         {
    25             MailMessage msg = new MailMessage();
    26             msg.To.Add("1234567@qq.com");//邮件接收者的帐号
    27             msg.From = new MailAddress("1234567@qq.com", "nickname", System.Text.Encoding.UTF8);//发送邮件的帐号及显示名称和字符编码
    28             msg.Subject = "Subject";//邮件标题 
    29             msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
    30             msg.Body = "Content";//邮件内容 
    31             msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
    32             msg.IsBodyHtml = false;//是否是HTML邮件 
    33             msg.Priority = MailPriority.High;//邮件优先级
    34   
    35             SmtpClient client = new SmtpClient();
    36             client.Credentials = new System.Net.NetworkCredential("1234567@qq.com", "password");//注册的邮箱和密码,就QQ邮箱而言,如果设置了独立密码,要用独立密码代替密码             
    37             client.Host = "smtp.qq.com";//QQ邮箱对应的SMTP服务器
    38             object userState = msg;
    39             try
    40             {
    41                 client.SendAsync(msg, userState);
    42                 return true;
    43             }
    44             catch (Exception ex)
    45             {
    46                 Console.WriteLine(ex.Message);
    47                 return false;
    48             }
    49         }
    50     }
    51 }

    WPF:

     1    using System;
     2    using System.Collections.Generic;
     3    using System.Linq;
     4    using System.Net.Mail;//添加引用
     5    using System.Text;
     6    using System.Windows;
     7    using System.Windows.Controls;
     8    using System.Windows.Data;
     9    using System.Windows.Documents;
    10    using System.Windows.Input;
    11    using System.Windows.Media;
    12    using System.Windows.Media.Imaging;
    13    using System.Windows.Navigation;
    14    using System.Windows.Shapes;
    15    
    16    namespace WPFSendMail
    17    {
    18        /// <summary>
    19        /// MainWindow.xaml 的交互逻辑
    20        /// </summary>
    21        public partial class MainWindow : Window
    22        {
    23            public MainWindow()
    24            {
    25                InitializeComponent();
    26            }
    27    
    28            private void Button_Click(object sender, RoutedEventArgs e)
    29            {
    30                bool flag = SendMail();
    31                if (flag)
    32                {
    33                    MessageBox.Show("Success !","Tips"); 
    34                }
    35            }
    36    
    37            public bool SendMail()
    38            {
    39                MailMessage message = new MailMessage();
    40                message.To.Add("       @qq.com");
    41                message.From = new MailAddress("       @qq.com","nickname",Encoding.UTF );
    42                message.Subject = "Sending e-mail in WPF !";
    43                message.SubjectEncoding = Encoding.UTF ;
    44                message.Body = "Awesome";
    45                message.BodyEncoding = Encoding.UTF ;
    46                message.IsBodyHtml = false;
    47                message.Priority = MailPriority.Normal;
    48                Attachment att = new Attachment("text.txt");//添加附件,确保路径正确
    49                message.Attachments.Add(att);
    50    
    51                SmtpClient smtp = new SmtpClient();
    52                smtp.Credentials = new System.Net.NetworkCredential("       @qq.com","password");
    53                smtp.Host = "smtp.qq.com";
    54                object userState = message;
    55    
    56                try
    57                {
    58                    smtp.SendAsync(message,userState);
    59                    return true;
    60                }
    61                catch (Exception ex)
    62                {
    63                    MessageBox.Show(ex.Message,"Tips");
    64                    return false;
    65                }
    66            }
    67        }
    68    }

    Winform:

     1    using System;
     2    using System.Collections.Generic;
     3    using System.Linq;
     4    using System.Text;
     5    using System.Web;
     6    using System.Web.UI;
     7    using System.Web.UI.WebControls;
     8    using System.Web.Mail;//添加引用
     9    
    10    namespace WebFormSendEmail
    11    {
    12        public partial class WebForm  : System.Web.UI.Page
    13        {
    14            protected void Page_Load(object sender, EventArgs e)
    15            {
    16    
    17            }
    18    
    19            protected void Button _Click(object sender, EventArgs e)
    20            {
    21                bool flag = SendMail();
    22                if (flag)
    23                {
    24                    Response.Write("<script>alert('Success !');</script>");   
    25                }
    26            }
    27    
    28            public bool SendMail()
    29            {
    30                MailMessage message = new MailMessage();
    31                message.To = "       @qq.com";
    32                message.From = "       @qq.com";
    33                message.Subject = "Sending e-mail in Web !";
    34                message.Body = "Awesome";
    35                //基本权限
    36                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", " ");
    37                //用户名
    38                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "       @qq.com");
    39                //密码
    40                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password"); 
    41    
    42                SmtpMail.SmtpServer = "smtp.qq.com";
    43                try
    44                {
    45                    SmtpMail.Send(message);
    46                    return true;
    47                }
    48                catch (Exception ex)
    49                {
    50                    Response.Write(ex.Message);
    51                    return false;
    52                }
    53            }
    54        }
    55    }

    MVC:

     1         try
     2         {
     3             WebMail.SmtpServer = "smtp.qq.com";
     4             WebMail.SmtpPort = 25;//端口号,不同SMTP服务器可能不同,可以查一下
     5             WebMail.EnableSsl = false;//禁用SSL
     6             WebMail.UserName = "1234567@qq.com";
     7             WebMail.Password = "password";
     8             WebMail.From = "1234567@qq.com";
     9             WebMail.Send("1234567@qq.com","Subject",“Content");
    10         }
    11         catch (Exception)
    12         {
    13            
    14         }  
  • 相关阅读:
    【NOIP2018】游记
    题解 P1441 【砝码称重】
    题解 P3128 【[USACO15DEC]最大流Max Flow】
    题解 P1949 【聪明的打字员_NOI导刊2011提高(10)】
    题解 P1966 【火柴排队】
    题解 P1895 【数字序列】
    topcoder做题
    1149E
    hdu 6589
    hdu 6579
  • 原文地址:https://www.cnblogs.com/rogerschong/p/4195889.html
Copyright © 2011-2022 走看看