Send Email using Gmail with Attachment

Contoh fungsi mengirim email menggunakan Gmail

[code language=”csharp”]
private void SendDispositionEmail(IncomingDocumentFormStub form)
{
//kamus
var message = new MailMessage();
string body = "<p>Nomor Surat: {0}</p><p>Perihal: {1}</p><p>Melihat detail: {2}</p>";
string link = "<a href=\"{0}\">{1}</a>";
string address = Request.Url.Scheme + "://" + HttpContext.Request.Url.Authority + Url.Action("Detail", "IncomingDocument", new { id = form.Id });
string[] attachmentPathList;
//address = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, System.Web.Routing.RouteTable.Routes, "klik di sini");

//algoritma
try
{
if (form.Disposition1Email != null || form.Disposition2Email != null)
{
if (form.Disposition1Email != null)
message.To.Add(new MailAddress(form.Disposition1Email));
if(form.Disposition2Email != null)
message.To.Add(new MailAddress(form.Disposition2Email));

link = string.Format(link, address, address);

message.Subject = "[Mandiri SMC – DMS] Disposisi Surat Masuk " + form.DocumentCode;
message.Body = string.Format(body, form.DocumentCode, form.Description, link);

if (form.AttachmentPath != null)
{
attachmentPathList = form.AttachmentPath.Split(new string[] { "<>" }, StringSplitOptions.RemoveEmptyEntries);

foreach (string attachmentPath in attachmentPathList)
{
message.Attachments.Add(new Attachment(HttpContext.Server.MapPath(attachmentPath)));
}
}

message.From = new MailAddress("rekadia.id@gmail.com");
message.IsBodyHtml = true;

using (var smtp = new SmtpClient())
{
smtp.Send(message);
}
}
}
catch (Exception ex)
{
string a = ex.ToString();
}
}

[/code]

Setting di web.config

[code language=”xml”]
<system.net>
<mailSettings>
<smtp from="rekadia.id@gmail.com">
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="rekadia.id@gmail.com" password="rekadian123" defaultCredentials="false" />
</smtp>
</mailSettings>
</system.net>

[/code]

http://www.mikesdotnetting.com/Article/268/how-to-send-email-in-asp-net-mvc

Chandra Oemaryadi has written 244 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>