How to send email in asp.net mvc with attachment
.Net
How to send email in asp.net mvc with attachment
Step1
Create a class and set properties those will be used to support email system
[Serializable]
public class EmailMessageObject
{
private string _fromemail;
private string _fromname;
private string[] _mailto;
private string _subject;
private string _body;
private string[] _ccemail;
private string[] _bccemail;
public List<string> AttachmentFiles { get; set; }
public string FromEmail
{
get { return _fromemail; }
set { _fromemail = value; }
}
public string[] CCEmail
{
get { return _ccemail; }
set { _ccemail = value; }
}
public string[] BCCEmail
{
get { return _bccemail; }
set { _bccemail = value; }
}
public string FromName
{
get { return _fromname; }
set { _fromname = value; }
}
public string[] MailTo
{
get { return _mailto; }
set { _mailto = value; }
}
public string Subject
{
get { return _subject; }
set { _subject = value; }
}
public string Body
{
get { return _body; }
set { _body = value; }
}
}
Step 2
Create a function that will send email with some parameters
public static bool SendEmail(EmailMessageObject emailMessage)
{
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential basicCredential = new NetworkCredential("emailid@gmail.com", "pasword");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("youremail@gmail.com", "webname");
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = emailMessage.Subject;
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = emailMessage.Body;
if (emailMessage.AttachmentFiles!=null)
{
foreach (var item in emailMessage.AttachmentFiles)
{
//string attachmentName = item;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(item);
message.Attachments.Add(attachment);
}
}
foreach (var item in emailMessage.MailTo)
{
message.To.Add(item);
}
try
{
smtpClient.Send(message);
return true;
}
catch (Exception ex)
{
return false;
//Error, could not send the message
// Response.Write(ex.Message);
}
}
How to send email in asp.net mvc with attachment
Step1
Create a class and set properties those will be used to support email system
[Serializable]
public class EmailMessageObject
{
private string _fromemail;
private string _fromname;
private string[] _mailto;
private string _subject;
private string _body;
private string[] _ccemail;
private string[] _bccemail;
public List<string> AttachmentFiles { get; set; }
public string FromEmail
{
get { return _fromemail; }
set { _fromemail = value; }
}
public string[] CCEmail
{
get { return _ccemail; }
set { _ccemail = value; }
}
public string[] BCCEmail
{
get { return _bccemail; }
set { _bccemail = value; }
}
public string FromName
{
get { return _fromname; }
set { _fromname = value; }
}
public string[] MailTo
{
get { return _mailto; }
set { _mailto = value; }
}
public string Subject
{
get { return _subject; }
set { _subject = value; }
}
public string Body
{
get { return _body; }
set { _body = value; }
}
}
Step 2
Create a function that will send email with some parameters
public static bool SendEmail(EmailMessageObject emailMessage)
{
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential basicCredential = new NetworkCredential("emailid@gmail.com", "pasword");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("youremail@gmail.com", "webname");
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = emailMessage.Subject;
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = emailMessage.Body;
if (emailMessage.AttachmentFiles!=null)
{
foreach (var item in emailMessage.AttachmentFiles)
{
//string attachmentName = item;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(item);
message.Attachments.Add(attachment);
}
}
foreach (var item in emailMessage.MailTo)
{
message.To.Add(item);
}
try
{
smtpClient.Send(message);
return true;
}
catch (Exception ex)
{
return false;
//Error, could not send the message
// Response.Write(ex.Message);
}
}
Share This with your friend by choosing any social account
You may also read following recent Post
![]() |
How old are you and what is your net worth? Also, what is your educational background and what sort of a job do you do? Do you enjoy your work?
47 By Nauman Shafi |
![]() |
create appointment booking page with available time slot using database ms sql server
50 By Junaid A |
![]() |
what is Asp.Net Core
64 By Junaid A |
![]() |
publish has encountered an error Object reference not set to an instance of an object A diagnostic log has been written to following location
96 By Junaid A |
![]() |
The provided URI scheme 'http' is invalid; expected 'https'." & vbCrLf & "Parameter name: via
105 By Junaid A |
![]() |
C# Language Basics
1667 By |
![]() |
Crud in Asp.NET using tabs
635 By Haider |
![]() |
Parser Error
699 By Usman Jafar |
![]() |
What are 3 C
192 By |
![]() |
what is .net
418 By |
![]() |
How to post uploaded file and form data in MVC using jquery?ts Common scenario where you want to pos
225 By |
![]() |
MVC url routing
263 By |
![]() |
Learn about Session
288 By |