ASP.NET - how to send - Asked By Bala Marish on 20-May-11 04:18 AM



how to send html greetings to email using asp.net.tell me some good links and some flows.
dipa ahuja replied to Bala Marish on 20-May-11 04:25 AM
protected void btnSent1_Click(object sender, EventArgs e)
  {
    string toEmailAddress = "xx@gmail.com";
    string GmailId = "abc@gmail.com";
    string password = "";
    string bodyMsg = "<html><body><img src='http://bolstablog.files.wordpress.com/2009/09/happy-birthday-card.jpg'/></body></html>";
 
    MailMessage mail = new MailMessage();
    mail.To.Add(toEmailAddress);
 
    mail.From = new MailAddress(GmailId);
    mail.Subject = txtSubject.Text;
    mail.Body = bodyMsg;
    mail.IsBodyHtml = true;
 
    //OR attatch from your computer
 
    if (FileUpload1.HasFile)
    {
      mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
    }
 
    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
    smtp.Send(mail);
  }
Jem Savery replied to Bala Marish on 20-May-11 04:29 AM

Hi Marish, see the following code, 

protected void yourButton_Click(object sender, EventArgs e)
    {
       
        string strMailContent = "Welcome new user";
        string fromAddress = "yourname@yoursite.com";
        string toAddress = "newuser@hisdomain.com";
        string contentId  = "image1";
        string path = Server.MapPath(@"images/Logo.jpg"); // my logo is placed in images folder
        MailMessage mailMessage = new MailMessage( fromAddress, toAddress );
        mailMessage.Bcc.Add("inkrajesh@hotmail.com"); // put your id here
        mailMessage.Subject = "Welcome new User";
      

        LinkedResource logo = new LinkedResource(path);
        logo.ContentId = "companylogo";
   // done HTML formatting in the next line to display my logo
        AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);
        av1.LinkedResources.Add(logo);

        mailMessage.AlternateViews.Add(av1);
        mailMessage.IsBodyHtml = true;
        SmtpClient mailSender = new SmtpClient("localhost"); //use this if you are in the development server
              mailSender.Send(mailMessage);
      
      }   

 Note:SmtpClient mailSender = new SmtpClient(ConfigurationManager.AppSettings["MyCustomId"]); // use this in the Production Server. I have specified my email server in the web.config file

Ref : http://www.codeproject.com/KB/aspnet/inkrajesh.aspx

Hope this helps.

Mahendar Nanamala replied to Bala Marish on 20-May-11 04:39 AM

You have asked some links refer below link:

http://www.search-this.com/2009/02/05/aspnet-sending-email-both-in-html-and-plain-text/


 


    public void SendMail()
    {
        MailMessage msgMail = new MailMessage();
      
        msgMail.To =TextBox1.Text;
        msgMail.From = "HR IN";
        msgMail.Subject = "Regarding Enquiry";
        System.Net.Mail.Attachment attachment;
       
                msgMail.BodyFormat = MailFormat.Html;
        string strBody = "<html><body><b>Hi" + TextBox2.Text.ToUpper() + "</b></br><br/>" +
           " <font color=\"red\">Greetings from Interactive Head!</font><br/><br/>We got your query.
 We will get back to you soon.<br/>" +
           "Thanks and Regards<br/>Interactive Head<br>NH-24 Shanti Nagar<br/>Near Kamla Hall,<br/>
 Ghaziabad<br>Uttar Pradesh , India <br/>Contact no.: +91 97-170-09112</body></html>";
        msgMail.Body = strBody;
        SmtpMail.SmtpServer = "smtp.gmail.com";
        msgMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
// your Email id
        msgMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "achiver1234@gmail.com");
// Your password
        msgMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "9897851861");
        msgMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
        msgMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
        SmtpMail.Send(msgMail);


    }

protected void Button1_Click(object sender, EventArgs e)
    {
        SendMail();
    }