Friday, November 25, 2011

Sending E-mail with attachments

[sourcecode language="csharp"]
using System;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;

namespace email4
{
class Program
{
static void Main(string[] args)
{
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();

try
{
m.From = new MailAddress("sender_email_address", "sender_display_name");
m.To.Add(new MailAddress("receiver_email_address", "receiver_display_name"));
m.CC.Add(new MailAddress("receiver_email_address", "receiver_display_name"));
//similarly BCC
m.Subject = "Test1";
m.IsBodyHtml = true;
m.Body = " This is a Test Mail";

FileStream fs = new FileStream("C:\\Documents and Settings\\All Users\\Documents\\test.pdf",
FileMode.Open, FileAccess.Read);
Attachment a = new Attachment(fs, "test.pdf",
MediaTypeNames.Application.Octet);
m.Attachments.Add(a);

sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new
System.Net.NetworkCredential("gmail_account_id", "gmail_account_password");
sc.EnableSsl = true;
sc.Send(m);

Console.ReadLine();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);

}
}
}
}


[/sourcecode]

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...