如何使用C#发送邮件?

时间:2010-03-01 07:47:54

标签: c# asp.net email

我想将邮件发送到任何电子邮件地址,如何使用C#进行操作。我在当地的主人工作。

7 个答案:

答案 0 :(得分:8)

System.Net.Mail.MailMessage message=new System.Net.Mail.MailMessage(
                new MailAddress(EmailUsername), new MailAddress("toemailaddress"));

message.Subject = "Message Subject";   // E.g: My New Email
message.Body = "Message Body";         // E.g: This is my new email ... Kind Regards, Me

对于SMTP部分,您还可以使用SmtpClient

SmtpClient client = new SmtpClient(ServerIP);
client.Credentials = new System.Net.NetworkCredential(EmailUsername, EmailPassword);
client.Send(message);

请考虑接受一些答案。 0%的接受率不是很好。


编辑修复愚蠢的错误。让我没有先检查代码。

答案 1 :(得分:4)

您可以使用SmtpClient课程,并使用Send实例致电SendAsync(或MailMessage)。这两个类都在System.Net.Mail命名空间中。

SmtpClient的default constructor使用app / web.config中的configuration,但您可以使用other constructors指定所需的SMTP设置。

// using System.Net.Mail;

SmtpClient client = new SmtpClient();

MailMessage mm = new MailMessage()
{
    Subject = "Subject here",
    Body = "Body here"
};

mm.To.Add("email@tempuri.org");
mm.From = new MailMessage("from@tempuri.org");

client.Send(mm);

答案 2 :(得分:2)

只是补充一点,有一个非常好的网站你应该知道的一切 System.Net:Mail名称空间

它被称为:

  

http://www.SystemNetMail.com/

希望从那以后帮助我的人一直在帮助我。)

答案 3 :(得分:0)

如果您使用的是ASP.Net MVC,我建议您查看MvcMailer

答案 4 :(得分:0)

试试这个......

public static void Send(string pFrom, string pSubject, string pTo, string pBody)
{
    System.Net.Mail.MailMessage loMail = new System.Net.Mail.MailMessage();
    System.Net.NetworkCredential loCredencial = new System.Net.NetworkCredential(MAIL_USERNAME, MAIL_PASSWORD);
    loMail.To.Add(pTo);
    loMail.Subject = pSubject;
    loMail.From = new System.Net.Mail.MailAddress(pFrom);
    loMail.IsBodyHtml = true;
    loMail.Body = pBody;
    System.Net.Mail.SmtpClient loSmtp = new System.Net.Mail.SmtpClient(MAIL_SMTP);
    loSmtp.UseDefaultCredentials = false;
    loSmtp.Credentials = loCredencial;
    loSmtp.Port = MAIL_PORT;
    loSmtp.Send(loMail);
}

答案 5 :(得分:0)

这是发送带附件的电子邮件

using System.Net;
using System.Net.Mail;

public void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);
}

答案 6 :(得分:0)

使用它。

  private static void SendMail(string subject, string content)
    {
        try

        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("YOURMAİL");
            mail.To.Add("MAİLTO");
            mail.Subject = subject;
            mail.Body = content;
            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("YOURMAİL", "YOURMAİLPASSWORD");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
        }
        catch (Exception ex)
        {

        }
    }

别忘了添加---使用System.Net.Mail; ---