Using ASP.NET to Send Emails with Images by Gmail [Level::Basic]
How programatically send emails with images as attachements using Gmail account is the subject of this post.
Create a new ASP.NET project and add the following SYSTEM.NET tag in its web.config file:
<system.net>
<mailsettings>
<smtp>
<network port="587" host="smtp.gmail.com">
</smtp>
</mailsettings>
</system.net>
In the Default.aspx page add three text boxes for "From", "To" and "Subject", add a text area for "Body" of emails, add a FileUpload control for attaching images to the emails and place a button tiltled as "Send Email" for sending emails.
Now, navigate to the code view of the Default.aspx page and in the Click event of the "Send Email" button, write the following code:
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
/////''''''''''''''''''''''''''''''''''''''''''/////
/////''''''''''''''''''''''''''''''''''''''''''/////
///// Description: Source code to send emails with
///// images as attachments using Gmail account.
///// Author: G. R. Roosta
///// License: Free To Use (No Restriction)
/////''''''''''''''''''''''''''''''''''''''''''/////
/////''''''''''''''''''''''''''''''''''''''''''/////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
protected void btnSendEmail_Click(object sender, EventArgs e)
{if (fileUpload.HasFile)
{
// Create and Set a MailMessage Object.
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
mailMessage.To.Add(txtTo.Text.Trim());
mailMessage.Subject = txtSubject.Text.Trim();
// Create Two Views, One Plain Text and One HTML.System.Net.Mail.AlternateView plainTextView =
System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), null, "text/plain");
System.Net.Mail.AlternateView htmlView =
System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "<image src=cid:HDIImage>", null, "text/html");
// Add the Attached Image to the HTML version.System.Net.Mail.LinkedResource imageResource =
new System.Net.Mail.LinkedResource(fileUpload.PostedFile.FileName);
imageResource.ContentId = "HDIImage";
htmlView.LinkedResources.Add(imageResource);
// Add the Two Views to the Message.mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
// Set Your Network Credential.System.Net.NetworkCredential networkCredential =
new System.Net.NetworkCredential("PutYourEmailAddress@gmail.com", "PutYourPassword");
// Send Message.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = networkCredential;
smtpClient.Port = 587;
smtpClient.Send(mailMessage);
try
{
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
string message = ex.Message;
string innerExceptionMessage = ex.InnerException.Message;
// ...}
}
}
Run the project and start sending emails!
How programatically send emails with images as attachements using Gmail account is the subject of this post.
Create a new ASP.NET project and add the following SYSTEM.NET tag in its web.config file:
<system.net>
<mailsettings>
<smtp>
<network port="587" host="smtp.gmail.com">
</smtp>
</mailsettings>
</system.net>
In the Default.aspx page add three text boxes for "From", "To" and "Subject", add a text area for "Body" of emails, add a FileUpload control for attaching images to the emails and place a button tiltled as "Send Email" for sending emails.
Now, navigate to the code view of the Default.aspx page and in the Click event of the "Send Email" button, write the following code:
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
/////''''''''''''''''''''''''''''''''''''''''''/////
/////''''''''''''''''''''''''''''''''''''''''''/////
///// Description: Source code to send emails with
///// images as attachments using Gmail account.
///// Author: G. R. Roosta
///// License: Free To Use (No Restriction)
/////''''''''''''''''''''''''''''''''''''''''''/////
/////''''''''''''''''''''''''''''''''''''''''''/////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
protected void btnSendEmail_Click(object sender, EventArgs e)
{if (fileUpload.HasFile)
{
// Create and Set a MailMessage Object.
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
mailMessage.To.Add(txtTo.Text.Trim());
mailMessage.Subject = txtSubject.Text.Trim();
// Create Two Views, One Plain Text and One HTML.System.Net.Mail.AlternateView plainTextView =
System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), null, "text/plain");
System.Net.Mail.AlternateView htmlView =
System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "<image src=cid:HDIImage>", null, "text/html");
// Add the Attached Image to the HTML version.System.Net.Mail.LinkedResource imageResource =
new System.Net.Mail.LinkedResource(fileUpload.PostedFile.FileName);
imageResource.ContentId = "HDIImage";
htmlView.LinkedResources.Add(imageResource);
// Add the Two Views to the Message.mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
// Set Your Network Credential.System.Net.NetworkCredential networkCredential =
new System.Net.NetworkCredential("PutYourEmailAddress@gmail.com", "PutYourPassword");
// Send Message.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = networkCredential;
smtpClient.Port = 587;
smtpClient.Send(mailMessage);
try
{
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
string message = ex.Message;
string innerExceptionMessage = ex.InnerException.Message;
// ...}
}
}
Run the project and start sending emails!
No comments:
Post a Comment