如何在asp.net中发送邮件

时间:2010-03-18 06:36:46

标签: asp.net

HII  我试图通过编码asp

发送邮件

是否有任何外部API来发送邮件,如JAVA

给出一些提示 如果可能的样本代码!!

我使用vs 2005以及vs 2008

2 个答案:

答案 0 :(得分:3)

您可以使用SmtpClient课程。使用GMail SMTP的示例:

var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount@gmail.com", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("youraccount@gmail.com");
mail.To.Add("youraccount@gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);

更新: 雅虎的例子:

var client = new SmtpClient("smtp.mail.yahoo.com", 587);
client.Credentials = new NetworkCredential("youraccount@yahoo.com", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("youraccount@yahoo.com");
mail.To.Add("destaccount@gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);

答案 1 :(得分:0)

试试这个:

using System.Web.Mail;
private void SendMessage()

{

MailMessage mail = new MailMessage();

mail.To = txtTo.Text;

mail.From = txtFrom.Text;

mail.Subject = txtSubject.Text;

mail.Body = txtBody.Text;

SmtpMail.SmtpServer = "localhost";

SmtpMail.Send(mail);

}

如果想发送附件

添加以下代码

mail.Attachments.Add(new MailAttachment(@"C:\myFile.txt"));