Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts

Sending Email Asynchronously

Posted by Venkat | Labels: , ,

Good Morning to Everybody this is my First article.
Now i am going to see how to send email Asynchronously, actually we send email normally using asp.net Example check here..
Send Email Using ASP.NET

By default smtpClient has one method called SendAsync to send an email ansynchronously. so if you sending an email normally it will take some time for each mail sent. But if you send an email Asynchronously it will not wait for each mail sent it will do the other process , while mail sending is done at background process.
So Here is the code.

//creating mail message object
 MailMessage mailMessage = new MailMessage();
 mailMessage.From = new MailAddress("venkat@gmail.com");
 mailMessage.To.Add(new MailAddress("msdotnettechies@gmail.com"));
 mailMessage.CC.Add(new MailAddress("user1@gmail.com"));
 mailMessage.Bcc.Add(new MailAddress("user2@gmail.com"));
 mailMessage.Subject = "Email Checking Asynchronously";
 mailMessage.Body = "Email test asynchronous";
 mailMessage.IsBodyHtml = true;//to send mail in html or not

 SmtpClient smtpClient = new SmtpClient();//portno here
 smtpClient.Host = "smtp.gmail.com";
 smtpClient.EnableSsl = true ; //True or False depends on SSL Require or not
 smtpClient.Credentials = new NetworkCredential("yourGmail@gmail.com", "yourpassword");
 //smtpClient.UseDefaultCredentials = true; //true or false depends on you want to default credentials or not
 Object mailState = mailMessage;

 //this code adds event handler to notify that mail is sent or not
 smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
 try
 {
 smtpClient.SendAsync(mailMessage, mailState);
 }
 catch (Exception ex)
 {
 Response.Write(ex.Message);
 Response.Write(ex.StackTrace);
 }

// this is the event called on main method
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
 {
 MailMessage mailMessage = e.UserState as MailMessage;
 if (e.Cancelled || e.Error != null)
 {

 Response.Write(e.Error.Message);
 Response.Write(e.Error.StackTrace);
 }
 else
 {
 Response.Write("Email sent successfully");
 }
 } 
 

Ref :  http://jalpesh.blogspot.com/2010/02/how-to-send-mail-asynchronously-in.html

Sams Teach Yourself ASP.NET 3.5 in 24 Hours, Complete Starter Kit How to Train Your DragonHarry Potter Box Set

Email with Attachment

Posted by Venkat | Labels:

Now we will see , how to send an attachment to the email.

for the you have to create an instance for Attachment class and pass the filename and specify the media type information for an email message attachements and set the ContentDisposition which specify the file type whether its txt , doc, gif,jpg etc..,

Import NameSpace

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

Code

try
{

MailMessage mail = new MailMessage();

mail.To.Add("venkat@gmail.com");

mail.From = new MailAddress("admin@Support.com");

mail.Subject = "Testing Email Attachment";

string file = Server.MapPath("~/files/findemai_ontextfile.txt");

Attachment attachFile = new Attachment(file, MediaTypeNames.Application.Octet);

ContentDisposition disposition = attachFile.ContentDisposition;

mail.Attachments.Add(attachFile);

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

smtp.EnableSsl = true;

smtp.Credentials = new NetworkCredential("xxxxxxx@gmail.com", "*********");
s;

smtp.Send(mail);

}

catch (Exception ex)
{

Response.Write(ex.Message);

}

This link helps you :
http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx

Send Email using Vb.NET

Posted by Venkat | Labels: , ,

Now we have to discuss how to send email using GMAIL server.

Normally we can use , Gmail, Yahoo , etc.. some company having own mailserver so you have to contact your Admin to know your mailserver , id , pwd etc..

This is code to send email using Vb.Net

Imports System.Net.Mail

Protected  Sub Button1_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim mail As MailMessage =  New MailMessage()
mail.To.Add("admin@gmail.com")
mail.From = New MailAddress("admin@gmail.com")
mail.Subject = "Email using Gmail server "

dim Body as String = "Hi, this mail is to test sending mail"+
             "using Gmail in ASP.NET using vb.Net"
mail.Body = Body

mail.IsBodyHtml = True
Dim smtp As SmtpClient =  New SmtpClient()
smtp.Host = "smtp.gmail.com" 
smtp.EnableSsl = True 
smtp.Credentials = New System.Net.NetworkCredential
    ("YourUserName@gmail.com","YourGmailPassword")
smtp.Send(mail)
End Sub

Send Email using CSharp.Net

Posted by Venkat | Labels: ,

Now we have to discuss how to send email using GMAIL server.

Normally we can use , Gmail, Yahoo , etc.. some company having own mailserver so you have to contact your Admin to know your mailserver , id , pwd etc..

This is code to send email using Asp.net user port no is 587 or 465 it depends upon system

declare the port below the smtp server name

smtp.port=587

using System.Net.Mail;


//Now write this code in click event of button

//C# code

protected void Button1_Click(object sender, EventArgs e)
{
  MailMessage mail = new MailMessage();
  mail.To.Add("admin@gmail.com");
  mail.From = new MailAddress("admin@gmail.com");
  mail.Subject = "Email using Gmail";

  string Body = "Hi, this mail is to test sending mail"+
                "using Gmail in ASP.NET using C#";
  mail.Body = Body;

  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; 
  smtp.EnableSsl = true; 
  smtp.Credentials = new System.Net.NetworkCredential
       ("YourUserName@gmail.com","YourGmailPassword");
    smtp.Send(mail);
}

//you have to give your Gmail Username and password ..

Embedding the image with an Email

Posted by Venkat | Labels: , ,

We have to discuss about how to Embed an image to Email Previous Article we are discussed passing image URL directly to body tag of Email.

So , now we Embed a Image directly to Email

See the example...

try

{

MailMessage mail = new MailMessage();

mail.To.Add("user@gmail.com");

mail.From = new MailAddress("admin@support.com");

mail.Subject = "Test with Image";

string Body = "<b> Welcome to Embed image!</b><br><BR>Online resource for .net articles.<BR><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >";


AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");

LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\images\sample.jpg", "image/jpg");


imagelink.ContentId = "imageId";

imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

htmlView.LinkedResources.Add(imagelink);

mail.AlternateViews.Add(htmlView);

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

//specify your smtp server name/ip here
smtp.EnableSsl = true;

//enable this if your smtp server needs SSL to communicate
smtp.Credentials = new NetworkCredential("username", "password");

// smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

smtp.Send(mail);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

Embedding the image using image URL

Posted by Venkat | Labels: ,

Hi Good day to all

Here We have to discuss about , how to embed the image to an email actually , Embedding an image will affect performance so here , i am passing the image URL directly to the body tag

so , here the code..


try

{

MailMessage mail = new MailMessage();

mail.To.Add("user@gmail.com");

mail.From = new MailAddress("admin@support.com");

mail.Subject = "Test with Image";

string Body = "<b>Welcome to Pass Image URL to Email Body!</b><br><BR>Online resource for .net articles.<BR><img alt=\"\" hspace=0 src='http://www.idealsd.co.uk/images/addmenu.jpg' align=baseline border=0 >";


AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");

mail.AlternateViews.Add(htmlView);

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

//specify your smtp server name/ip here
smtp.EnableSsl = true;

//enable this if your smtp server needs SSL to communicate
smtp.Credentials = new NetworkCredential("Username", "password");

// smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

smtp.Send(mail);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

PayOffers.in