郵件發送_文本發送

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMail
{
    public partial class Form1 : Form
    {
        public string smtp = "smtp.163.com";//這個是163的格式。
        public string from = "fromMail@163.com";//發件人郵箱
        public string pwd = "fromMailPwd";//發件人密碼
        public string to = "toMail@163.com";//郵件接收人
        public string subject = "測試";//郵件標題
        public string body = "內容11111111111" +System.DateTime.Now;//郵件內容

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            SendMail2();
        }

        public void SendMail2()
        {
            //建立smtpclient對象
            System.Net.Mail.SmtpClient client = new SmtpClient();
            client.Host = smtp;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(from, pwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            //建立mailMessage對象 
            System.Net.Mail.MailMessage message = new MailMessage(from, to);
            message.Subject = subject;
            //正文默認格式爲html
            message.Body = body;
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;            

            try
            {
                client.Send(message);                
            }
            catch (Exception ex)
            {
               
            }
        }
    }
}
相關文章
相關標籤/搜索