开发者

how to send mail using C#?

i want to send mail开发者_JAVA百科 to any email address, how to do that using C#. i am working on local host.


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

For the SMTP part, you can also use SmtpClient:

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

Please consider accepting some answers. A 0% accepted rate is not great.


Edited to fix the silly mistakes. Serves me right for not checking the code first.


You can use the SmtpClient class and call Send (or SendAsync) with a MailMessage instance. Both these classes are in the System.Net.Mail namespace.

SmtpClient's default constructor uses the configuration from your app/web.config, but you can use other constructors to specify the SMTP settings you want.

// 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);


just to add that, there is a really nice website with everything you should know about System.Net:Mail namespace

it is called:

http://www.SystemNetMail.com/

hope it helps someone like it's been helping me ever since :)


This is to send Email with Attachment

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);
}


Try this...

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);
}


If you're using ASP.Net MVC I would recommend that you have a look at MvcMailer


Use this.

  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)
        {

        }
    }

And don't forget to add ---using System.Net.Mail;---

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜