Pages

Wednesday, June 1, 2011

Kirim Email ASP.NET (C#)

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

public bool KirimEmail(List<string> recipients, string sub,string textbody)
{       
        MailMessage mail = new MailMessage();

        foreach (var item in recipients)
        {
            mail.To.Add(item);
        }

        mail.From = new MailAddress("AlamatEmailAnda@Gmail.com");

        mail.Subject = sub;
        //email's body, this is going to be html.
        //note that we attach the image as using cid
        mail.Body = textbody; //<br/> &lt;img src=\ " cid:tmpImage.gif\ " >";
        //set email's body to html
        mail.IsBodyHtml = true;

        //add our attachment
        //Attachment imgAtt = new Attachment(Server.MapPath("tmpImage.gif"));
 //give it a content id that corresponds to the src we added in the body img tag
        //imgAtt.ContentId = "tmpImage.gif";
        //add the attachment to the email
       // mail.Attachments.Add(imgAtt);

        //setup our smtp client, these are Gmail specific settings
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true; //ssl must be enabled for Gmail
        //our Gmail account credentials
NetworkCredential credentials = new NetworkCredential("AlamatEmailAnda@Gmail.com", "pass");
        //add credentials to our smtp client
        client.Credentials = credentials;

        try
        {
            client.Send(mail);
            return true;
        }
        catch
        {
            return false;
        }
}

No comments:

Post a Comment

ShareThis