.NET使用Gmail SMTP發信
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Web.Mail;
namespace MyGmail
{
///
/// Description of MainForm.
///
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox strEmailTo;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RichTextBox strBody;
private System.Windows.Forms.TextBox strSubject;
private System.Windows.Forms.TextBox strEmailFrom;
private System.Windows.Forms.Button SendBtn;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
///
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
///
private void InitializeComponent() {
this.SendBtn = new System.Windows.Forms.Button();
this.strEmailFrom = new System.Windows.Forms.TextBox();
this.strSubject = new System.Windows.Forms.TextBox();
this.strBody = new System.Windows.Forms.RichTextBox();
this.label4 = new System.Windows.Forms.Label();
this.strEmailTo = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// SendBtn
//
this.SendBtn.Location = new System.Drawing.Point(304, 392);
this.SendBtn.Name = "SendBtn";
this.SendBtn.Size = new System.Drawing.Size(136, 32);
this.SendBtn.TabIndex = 8;
this.SendBtn.Text = "傳送信件";
this.SendBtn.Click = new System.EventHandler(this.SendBtnClick);
//
// strEmailFrom
//
this.strEmailFrom.Location = new System.Drawing.Point(72, 8);
this.strEmailFrom.Name = "strEmailFrom";
this.strEmailFrom.Size = new System.Drawing.Size(200, 22);
this.strEmailFrom.TabIndex = 4;
this.strEmailFrom.Text = "";
//
// strSubject
//
this.strSubject.Location = new System.Drawing.Point(72, 72);
this.strSubject.Name = "strSubject";
this.strSubject.Size = new System.Drawing.Size(352, 22);
this.strSubject.TabIndex = 6;
this.strSubject.Text = "";
//
// strBody
//
this.strBody.Location = new System.Drawing.Point(8, 144);
this.strBody.Name = "strBody";
this.strBody.Size = new System.Drawing.Size(432, 240);
this.strBody.TabIndex = 7;
this.strBody.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 120);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 3;
this.label4.Text = "信件內容";
//
// strEmailTo
//
this.strEmailTo.Location = new System.Drawing.Point(72, 40);
this.strEmailTo.Name = "strEmailTo";
this.strEmailTo.Size = new System.Drawing.Size(200, 22);
this.strEmailTo.TabIndex = 5;
this.strEmailTo.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 0;
this.label1.Text = "寄件人";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 1;
this.label2.Text = "收件人";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 23);
this.label3.TabIndex = 2;
this.label3.Text = "主旨";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(448, 438);
this.Controls.Add(this.SendBtn);
this.Controls.Add(this.strBody);
this.Controls.Add(this.strSubject);
this.Controls.Add(this.strEmailTo);
this.Controls.Add(this.strEmailFrom);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "MainForm";
this.Text = "MyGmail發信程式";
this.ResumeLayout(false);
}
#endregion
void SendBtnClick(object sender, System.EventArgs e)
{
MailMessage MyMail=new 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","Demo");
//帳號密碼 (Demo自行填入)
MyMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","Demo");
MyMail.From=strEmailFrom.Text;
MyMail.To=strEmailTo.Text;
MyMail.Subject=strSubject.Text;
MyMail.BodyFormat=MailFormat.Text;
MyMail.Body=strBody.Text;
//SmtpMail.SmtpServer="smtp.gmail.com"; //指定SMTP另一寫法
SmtpMail.Send(MyMail);
}
}
}
網海無涯,唯學是岸!