Asp.net 2.0 傳送Email(Using System.web.Mail)

dzy5858發表於2007-11-20

下面的code都是經過我驗證過的,肯定可以用:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
public partial class send_email: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

string filename = this.FileUpload1.PostedFile.FileName;
string sAttach = filename;

try
{
MailMessage msg = new MailMessage();
msg.From = "";
msg.To = "";
msg.Subject = "test email";
msg.Body = "can you receive this email?";
msg.Priority = MailPriority.High;
msg.BodyFormat = MailFormat.Html;

// Build an IList of mail attachments.
if (sAttach != "")
{
MailAttachment attachment = new MailAttachment(sAttach);
msg.Attachments.Add(attachment);
}

SmtpMail.SmtpServer = "smtp.gmail.com";

//1 代表使用 local smtp, 2 外部 smtp
msg.Fields.Add("", 2);

//SMTP Server
msg.Fields.Add("", "smtp.gmail.com");


//Server port, gmail is 465
msg.Fields.Add("", "465");

//Authentication method, ssl or not, Username and password for the SMTP Server
msg.Fields.Add("", 1);

//cdoBasic 基本驗證 gmail使用ssl驗證
msg.Fields.Add("", true);

//賬號
msg.Fields.Add("", "dzy");

//密碼
msg.Fields.Add("", "123456");

SmtpMail.Send(msg);
Response.Write("Successed!!");
}
catch (Exception ex)
{
throw ex;
}
}
}

[@more@]A

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/107297/viewspace-983057/,如需轉載,請註明出處,否則將追究法律責任。

相關文章