C# .NET - sending mail message - Asked By prince b on 29-Jun-12 04:59 AM

S K replied to prince b on 29-Jun-12 05:02 AM
There are lots of good thread on eggheadcafe itself to discuss how to send email using code
see some of them
http://www.eggheadcafe.com/community/visual-studio/7/10065297/send-email.aspx 
http://www.eggheadcafe.com/community/asp-net/17/10349260/how-can-i-imlement-email-funcationality.aspx 

hope this helps you
Jitendra Faye replied to prince b on 29-Jun-12 05:32 AM
For sending mail you have to use MailMessage Class.

you can include image in message body-

follow this code

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
LinkedResource myimage = new LinkedResource(FileName);
// Create HTML view
AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
// Set ContentId property. Value of ContentId property must be the same as
// the src attribute of image tag in email body. 
myimage.ContentId = "companylogo";
htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{

}
}

RAJASEKHAR RAJENDRAN replied to prince b on 29-Jun-12 06:08 AM
Hi Prince,

The following links helps a lot by basic tutorials 

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx#Y2777 



Apart from the above the below link might be useful.

http://archive.msdn.microsoft.com/sendemail 
http://social.msdn.microsoft.com/forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8/ 
http://www.eggheadcafe.com/community/visual-studio/7/10065297/send-email.aspx 
http://www.eggheadcafe.com/community/asp-net/17/10349260/how-can-i-imlement-email-funcationality.aspx  

Thanks & Regards,
Rajasekhar.R
Lalit M replied to prince b on 29-Jun-12 06:18 AM
Use this code sample
----------
private void btnSend_Click(object sender, System.EventArgs e)

{

    MailMessage msg = new MailMessage();

    msg.To = txtTo.Text;

    msg.From = txtFrom.Text;

    msg.Subject = txtSubject.Text;

    msg.Body = txtContent.Value;
 
    lblStatus.Text = "Sending...";

    SmtpMail.SmtpServer = "smtp.yourISP.com";

    SmtpMail.Send(msg);

    lblStatus.Text = "Sent email (" + txtSubject.Text + ") to " + txtTo.Text;
}

Read more
---------
http://parassanghani.blogspot.in/2011/02/sending-emails-in-cnet.html
http://www.dreamincode.net/forums/topic/57355-sending-e-mail-messages-using-c%23/
dipa ahuja replied to prince b on 29-Jun-12 09:31 AM
using System.Net.Mail;
using System.Net;
 
private void button1_Click(object sender, EventArgs e)
    {
      string Sendto = "abc@gmail.com"; //Email Address to reciever
      string UserName = ""; //Ur Gmail address
      string PassWord = ""; //Gmail password
      NetworkCredential loginInfo = new NetworkCredential(UserName, PassWord);
      MailMessage msg = new MailMessage();
      msg.From = new MailAddress(UserName);
      msg.To.Add(new MailAddress(to.ToString()));
      msg.Subject = "Testing Mail";
      msg.Body = textBox1.Text.ToString();
      msg.IsBodyHtml = true;
      SmtpClient client = new SmtpClient("smtp.gmail.com");
      client.EnableSsl = true;
      client.UseDefaultCredentials = false;
      client.Credentials = loginInfo;
      client.Send(msg);
 
      MessageBox.Show("mail sent");
    }