1. 程式人生 > >c# 自動傳送郵件測試程式碼

c# 自動傳送郵件測試程式碼

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public string strFromMailbox = "
[email protected]
"; public string strToMailbox = "[email protected]"; public string strCCMailbox = "[email protected]"; public string strShowName = "antony"; public string strPassword = "XXXXXXXX"; Encoding myEncoding=System.Text.Encoding.UTF8; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.To.Add(strToMailbox); msg.CC.Add(strCCMailbox); msg.From = new MailAddress(strFromMailbox, strShowName, myEncoding); /* 上面3個引數分別是發件人地址(可以隨便寫),發件人姓名,編碼*/ msg.Subject = "這是測試郵件";//郵件標題 msg.SubjectEncoding = myEncoding;//郵件標題編碼 msg.Body = "郵件內容:hello world!";//郵件內容 msg.BodyEncoding = myEncoding;//郵件內容編碼 msg.IsBodyHtml = false;//是否是HTML郵件 msg.Priority = MailPriority.High;//郵件優先順序 SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential(strFromMailbox, strPassword); //上述寫你的郵箱和密碼 client.Port = 465;//郵箱使用的埠 client.Host = "smtp.263.net"; client.EnableSsl = true;//經過ssl加密 object userState = msg; try { client.SendAsync(msg, userState); //簡單一點兒可以client.Send(msg); MessageBox.Show("傳送成功"); } catch (System.Net.Mail.SmtpException ex) { MessageBox.Show(ex.Message, "傳送郵件出錯"); } } } }