3
Answers

how can i send mail c#

malar sha

malar sha

8y
379
1
How can i send mail in c# application
Answers (3)
1
Gokul

Gokul

NA 577 22.5k 8y
using System; using System.Windows.Forms; 
using System.Net.Mail;  //add names space
namespace WindowsApplication1 
{  
   public partial class Form1 : Form 
    {  
       public Form1()   
      {         
    InitializeComponent();   
      }      
    private void button1_Click(object sender, EventArgs e)   
      {     
        try        
     {         
        MailMessage mail = new MailMessage();   
              SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");    
              mail.From = new MailAddress("your_email_address@gmail.com");                
 mail.To.Add("to_address");                 
mail.Subject = "Test Mail";                 
mail.Body = "This is for testing SMTP mail from GMAIL";                 
 SmtpServer.Port = 587;                 
SmtpServer.Credentials = new System.Net.NetworkCredential("username//yourmail", "password//yourpassword");    
             SmtpServer.EnableSsl = true; 
                 SmtpServer.Send(mail);
                 MessageBox.Show("mail Send");  
           }    
         catch (Exception ex)       
      {              
   MessageBox.Show(ex.ToString());             
}         
}   
} 
}
Accepted
1
Ananth G

Ananth G

NA 2.8k 124.8k 8y
Hi Friends,
  • Below i give dynamic code for sending mail with Html format.
  • i give sample HTML mail format. you will change that html format based on your requirement
  • Two way to send mail .
  1. Use your gmail Id and password.
  2. UseSmtpClient URL and Port Number.
  • I give Both two Way If you haveSmtpClient URL and Port Number then you enable#region Not_Have_SmtpClient code else#region Have_SmtpClient code
private static string FunForMailSending()
{
//IF You don't have SmtpClient Url and port No then you must give your gmail id and //paswword.
SendHtmlFormattedEmail("Your Subject", "From GMAIL MailID", "GMAIL ID Pwd", "To MailID", "Bcc MailID", "CC MailID", FileUploadControlID);
}
public void SendHtmlFormattedEmail(string objSubject,string objFromMailID, string objPwd, string objToMailID, string objBccMailID, string objCCMailID, FileUpload fuAttachment)
{
try
{
MailMessage objMailMsg;
objMailMsg = new MailMessage();
objMailMsg.From = new MailAddress(objFromMailID);
objMailMsg.Bcc.Add(objBccMailID);
objMailMsg.Bcc.Add(objCCMailID);
objMailMsg.To.Add(objToMailID);
objMailMsg.Subject = objSubject;
objMailMsg.Body = FunForHtmlMailFormat("YOUR PRODUCT_ID", "YOUR PRODUCT_CODE");
if (fuAttachment.HasFile)
{
string stringFileName = System.IO.Path.GetFileName(fuAttachment.PostedFile.FileName);
objMailMsg.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, stringFileName));
}
#region Have_SmtpClient
////If you did have SmtpClient Url and port means you will used below code else command below //code enable #region Not_Have_SmtpClient code
//SmtpClient objSmtpClient = new SmtpClient(objAutomailURL, Convert.ToInt32(objAutomailPort));
//objSmtpClient.Host = objAutomailURL;
//objSmtpClient.Port = Convert.ToInt32(objAutomailPort);
//objSmtpClient.UseDefaultCredentials = false;
//objSmtpClient.Send(objMailMsg);
//objSmtpClient.Dispose();
#endregion
#region Not_Have_SmtpClient
////If you did not have SmtpClient Url and port means you will used below code else command below code enable #region Have_SmtpClient code
SmtpClient objSmtpClient = new SmtpClient("smtp.gmail.com");
objSmtpClient.Port = 587;
objSmtpClient.EnableSsl = true;
objSmtpClient.UseDefaultCredentials = false;
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
objSmtpClient.Credentials = new System.Net.NetworkCredential(objFromMailID, objPwd);
objSmtpClient.Send(objMailMsg);
objSmtpClient.Dispose();
#endregion
}
catch (Exception ex)
{
throw ex;
}
}
private static string FunForHtmlMailFormat(string objID, string objName)
{
return " < html >< body >< div style = 'font-family:Calibri; font-size: 14px; width: 100%!important; min-height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0' >< table style = 'font-family: Calibri; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0' >< tbody >< tr style = 'font-family: Calibri; font-size: 14px; margin: 0' >< td style = 'font-family: Calibri; font-size: 14px; vertical-align: top; margin: 0' ></ td >< td style = 'width: 600px; font-family: Calibri; font-size: 14px; vertical-align: top; display: block!important; max-width: 600px!important; clear: both!important; margin: 0 auto' >< div style = 'font-family: Calibri; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px' >< table style = 'width: 100%; font-family: Calibri; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9' >< tbody >< tr style = 'font-family: Calibri; font-size: 14px; margin: 0' >< td style = 'font-family: Calibri; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: left; border-radius: 3px 3px 0 0; background-color: #00ACAC; margin: 0; padding: 20px' >< label style = 'font-size:36px;font-family:Impact;color:black;' > YOUR APPLICATION NAME</ label ></ td ></ tr >< tr style = 'font-family: Calibri; font-size: 14px; margin: 0' >< td style = 'font-family: Calibri; font-size: 14px; vertical-align: top; margin: 0; padding: 20px' >< table style = 'width: 100%; font-family: Calibri; font-size: 14px; margin: 0' >< tbody >< tr style = 'font-family: Calibri; font-size: 14px; margin: 0' >< td style = 'font-family: Calibri; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px' > Dear All,</ td ></ tr >< tr style = 'font-family: Calibri; font-size: 14px; margin: 0' >< td style = 'font-family: Calibri; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px' >YOUR DESCRIPTION < table style = 'font-size: 12px; width: 480px; text-align: left; border-collapse: collapse; margin: 20px;'summary = 'Meeting Results' >< tbody >< tr style = 'display: table-row;' >< td style = 'color: #669; border: 1px solid #ddd; padding:5px;font-weight:bold;' > Product Id </ td >< td style = 'color: #669; border: 1px solid #ddd; padding:5px;' > " + objID + " </ td ></ tr >< tr >< td style = 'color: #669; border: 1px solid #ddd; padding:5px;font-weight:bold;' > Product Name </ td >< td style = 'color: #669; border: 1px solid #ddd; padding:5px;' > " + objName + " </ td ></ tr ></ tbody ></ table ></ td ></ tr >< tr style = 'font-family: Calibri; font-size: 14px; margin: 0' >< td style = 'font-family: Calibri; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px' > Thanking You,< br />< i >< span > YOUR SIGNATURE </ span > </ i >< br />< br /> Please do not reply to this mail - id, It is an automated mail.</ td ></ tr >< tr style = 'font-family: Calibri; font-size: 14px; margin: 0' >< td style = 'font-family: Calibri; font-size: 12px; line-height: 16px; color: #aaa; vertical-align: top; margin: 0; padding: 20px 0 20px; border-top: 1px #ccc solid; text-align: center;color:#008A8A;' > All rights reserved © YOUR COPY RIGHTS DETAILS 2016. </ td ></ tr ></ tbody ></ table ></ td ></ tr ></ tbody ></ table ></ div ></ td >< td style = 'font-family: Calibri; font-size: 14px; vertical-align: top; margin: 0' ></ td ></ tr ></ tbody ></ table ></ div ></ body ></ html >";
}
0
Rajeesh Menoth

Rajeesh Menoth

NA 24.7k 628.6k 8y
Hi,
 
You can refer my article related to sending email using C# and Related issues.
 
Code : 
  1. using System.IO;    
  2. using System.Net;    
  3. using System.Net.Mail;    
  4.     
  5.     
  6. string to = "toaddress@gmail.com"//To address      
  7. string from = "fromaddress@gmail.com"//From address      
  8. MailMessage message = new MailMessage(from, to);    
  9.     
  10. string mailbody = "In this article you will learn how to send a email using Asp.Net & C#";    
  11. message.Subject = "Sending Email Using Asp.Net & C#";    
  12. message.Body = mailbody;    
  13. message.BodyEncoding = Encoding.UTF8;    
  14. message.IsBodyHtml = true;    
  15. SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp      
  16. System.Net.NetworkCredential basicCredential1 = new    
  17. System.Net.NetworkCredential("yourmail id""Password");    
  18. client.EnableSsl = true;    
  19. client.UseDefaultCredentials = false;    
  20. client.Credentials = basicCredential1;    
  21. try     
  22. {    
  23.     client.Send(message);    
  24. }     
  25.     
  26. catch (Exception ex)     
  27. {    
  28.     throw ex;    
  29. }    
 
Reference :
 
http://www.c-sharpcorner.com/UploadFile/2a6dc5/how-to-send-a-email-using-Asp-Net-C-Sharp/