全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3242
推到 Plurk!
推到 Facebook!

從 CDO中透過 Gmail smtp 發信

 
qoo1234
版主


發表:256
回覆:1167
積分:659
註冊:2003-02-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-05 17:53:11 IP:220.131.xxx.xxx 未訂閱
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    strBody: TMemo;
    Button1: TButton;
    strEmailFrom: TEdit;
    strEmailTo: TEdit;
    strSubject: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation
  uses comobj,ActiveX;
{$R *.dfm}    procedure TForm1.Button1Click(Sender: TObject);
var
 MyMail:OleVariant;
begin
 try
  try
   CoInitialize(nil);
   MyMail:=CreateOleObject('CDO.Message');
   // 1 代表使用 local smtp, 2 為外部 smtp
   myMail.Configuration.Fields.Item('http://schemas.microsoft.com/cdo/configuration/sendusing') := 2;
   //SMTP Server domain name
   myMail.Configuration.Fields.Item('http://schemas.microsoft.com/cdo/configuration/smtpserver') := 'smtp.gmail.com';
   //Server port, gmail use ssl smtp authentication, port number is 465
   myMail.Configuration.Fields.Item('http://schemas.microsoft.com/cdo/configuration/smtpserverport') := 465;
   //Authentication method, ssl or not, Username and password for the SMTP Server
   myMail.Configuration.Fields.Item('http://schemas.microsoft.com/cdo/configuration/smtpauthenticate') := 1;
   //cdoBasic 基本驗證
   myMail.Configuration.Fields.Item('http://schemas.microsoft.com/cdo/configuration/smtpusessl') := true;       myMail.Configuration.Fields.Item('http://schemas.microsoft.com/cdo/configuration/sendusername') := 'Demo'; //帳號名稱 (自行填入)
   myMail.Configuration.Fields.Item('http://schemas.microsoft.com/cdo/configuration/sendpassword') := 'Demo'; //帳號密碼 (自行填入)
   myMail.Configuration.Fields.Update;       myMail.Subject := strSubject.Text;
   myMail.From := strEmailFrom.Text;
   myMail.To := strEmailTo.Text;
   myMail.TextBody := strBody.Text;
   myMail.Send;
   ShowMessage('傳送完成');
  except
   ShowMessage('傳送錯誤失敗');
  end;
  
  finally
   myMail:=null;
   CoUninitialize;
  end;
end;    end.     
網海無涯,唯學是岸!
qoo1234
版主


發表:256
回覆:1167
積分:659
註冊:2003-02-24

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-05 18:00:55 IP:61.219.xxx.xxx 未訂閱
.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);
  }
  
 }
}
 
網海無涯,唯學是岸!
qoo1234
版主


發表:256
回覆:1167
積分:659
註冊:2003-02-24

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-09-05 18:03:03 IP:61.219.xxx.xxx 未訂閱
參考:    如何從 asp 中透過 gmail smtp 發信    http://blog.yam.com/phanix/archives/428744.html    網海無涯,唯學是岸!
系統時間:2024-07-06 23:38:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!