Now throw this junk of code we can able to send email with embedded Image. Its a method you can call this method in any event
We take host name from web.config you can find the code below
protected void btnSendEmail_Click(object sender, EventArgs e)
{
if (fileImage.HasFile)
{
//Holds message information.
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
//Add basic information.
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 text, 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 image to HTML version
System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(fileImage.PostedFile.FileName);
imageResource.ContentId = "HDIImage";
htmlView.LinkedResources.Add(imageResource);
//Add two views to message.
mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);
//Send message
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Send(mailMessage);
}
< Configuring with web.congif file
< system.net>
< mailSettings>
< smtp>
< network
< host="YOUR HOST HERE" />
< /smtp>
< /mailSettings>
< /system.net>
Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts
How to Sending Email Asynchronously .net 3.5
Now throw this junk of code we can able to send email Asynchronously Its a method you can call this method in any event
We take host name from web.config you can find the code below
private void SendMail()
{
//Create message object and populate with the data from form
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
message.To.Add(txtTo.Text.Trim());
message.Subject = txtSubject.Text.Trim();
message.Body = txtBody.Text.Trim();
//Setup SmtpClient to send email. Uses web.config settings.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
//Error handling for sending message
try
{
smtpClient.Send(message);
//Exception contains information on each failed receipient
}
catch (System.Net.Mail.SmtpFailedRecipientsException recExc)
{
for (int recipient = 0; recipient < recExc.InnerExceptions.Length - 1; recipient++)
{
System.Net.Mail.SmtpStatusCode statusCode;
statusCode = recExc.InnerExceptions[recipient].StatusCode;
if ((statusCode == System.Net.Mail.SmtpStatusCode.MailboxBusy) || (statusCode == System.Net.Mail.SmtpStatusCode.MailboxUnavailable))
{
System.Threading.Thread.Sleep(5000);
smtpClient.Send(message);
}
else
{
ErrorLabel.Text = recExc.Message;
}
}
}
//General SMTP execptions
catch (System.Net.Mail.SmtpException smtpExc)
{
ErrorLabel.Text = smtpExc.StatusCode.ToString();
}
catch (Exception ex)
{
//Log error to event log.
}
}
Configuring web.config file
< system.net>
< mailSettings>
< smtp>
< network host="YOUR HOST INFO HERE"/>
< /smtp>
< /mailSettings>
< /system.net>
We take host name from web.config you can find the code below
private void SendMail()
{
//Create message object and populate with the data from form
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
message.To.Add(txtTo.Text.Trim());
message.Subject = txtSubject.Text.Trim();
message.Body = txtBody.Text.Trim();
//Setup SmtpClient to send email. Uses web.config settings.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
//Error handling for sending message
try
{
smtpClient.Send(message);
//Exception contains information on each failed receipient
}
catch (System.Net.Mail.SmtpFailedRecipientsException recExc)
{
for (int recipient = 0; recipient < recExc.InnerExceptions.Length - 1; recipient++)
{
System.Net.Mail.SmtpStatusCode statusCode;
statusCode = recExc.InnerExceptions[recipient].StatusCode;
if ((statusCode == System.Net.Mail.SmtpStatusCode.MailboxBusy) || (statusCode == System.Net.Mail.SmtpStatusCode.MailboxUnavailable))
{
System.Threading.Thread.Sleep(5000);
smtpClient.Send(message);
}
else
{
ErrorLabel.Text = recExc.Message;
}
}
}
//General SMTP execptions
catch (System.Net.Mail.SmtpException smtpExc)
{
ErrorLabel.Text = smtpExc.StatusCode.ToString();
}
catch (Exception ex)
{
//Log error to event log.
}
}
Configuring web.config file
< system.net>
< mailSettings>
< smtp>
< network host="YOUR HOST INFO HERE"/>
< /smtp>
< /mailSettings>
< /system.net>
Labels:
Email
Subscribe to:
Posts (Atom)