Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear friends,

I would like to use email facilities.
Can you tell me how to how to write code for that?
How to use SMTP and other service?

I have never use till now.
Please help me.

I would also like to write code for auto generated mail as per time set.
Posted
Updated 16-Nov-11 23:35pm
v3
Comments
André Kraak 17-Nov-11 5:33am    
Edited question:
Formatted text/code
Spelling/Grammar
Replaced txt speak

Try this[^].
 
Share this answer
 
sendEmail("Test Subject", "me@mail.com", "to@mail.com", "Body of email", "", "",null);

public static bool sendEmail(string strSubject, string strFrom, string strTo, string strBody, string strCc, string strBcc,string displayName)
{
System.Net.Mail.SmtpClient Mail = new System.Net.Mail.SmtpClient();
Mail.Host = clsCommon.value("SMTPServer").ToString();


string username = null;
string Password = null;

Mail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
username = "MailUserName";
Password = "MailPassword";
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(username, Password);

Mail.UseDefaultCredentials = false;
Mail.Credentials = basicAuthenticationInfo;



System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
myMail.Subject = strSubject;

myMail.From = new System.Net.Mail.MailAddress(strFrom,displayName);
myMail.To.Add(new System.Net.Mail.MailAddress(strTo));
if (!string.IsNullOrEmpty(strCc))
{
myMail.CC.Add(new System.Net.Mail.MailAddress(strCc));
}
if (!string.IsNullOrEmpty(strBcc))
{
myMail.Bcc.Add(new System.Net.Mail.MailAddress(strBcc));
}

myMail.IsBodyHtml = true;
myMail.Body = strBody;
try
{

Mail.Send(myMail);


return true;
}
catch (Exception ex)
{
ex.Message.ToString();
return false;
}
}
 
Share this answer
 
Comments
Richard MacCutchan 17-Nov-11 15:03pm    
Please use <pre> tags around your code snippets.
C#
namespace System.net.mail;


you have to write this code when you click the button you want to send mail that button click event.

string s1="m.kalaingar@gmail.com";
string s2 = TextBox1.Text;
string s3 = TextBox2.Text;
string body = "dfhfhkjsdhgfjksdfhkjsfgjksfjksdhfsdfaskjfsdjkfh";
MailMessage mail = new MailMessage();
mail.From = new MailAddress(s1);
mail.To.Add(s2);
mail.Subject = s3;
mail.Body = body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
Response.Write("your message send successfully");

you have to add this code to your webconfig

XML
<system.net>
    <mailSettings>


<smtp from="m.kalaingar@gmail.com">
<network host="smtp.gmail.com" port="465" username="yourgmailid" password="yourpassword">

 
Share this answer
 
Comments
Richard MacCutchan 17-Nov-11 15:03pm    
Please use <pre> tags around your code snippets.
 
Share this answer
 
Try this code :)

C#
if ((!string.IsNullOrEmpty(txtTo.Text) & !string.IsNullOrEmpty(txtFrom.Text) & !string.IsNullOrEmpty(txtAttach.Text) & !string.IsNullOrEmpty(txtSubject.Text))) {
	try {
		string File = null;
		SmtpClient mailClient = new SmtpClient();
		string msgFrom = null;
		string msgTo = null;
		string msgBody = null;
		string msgSubject = null;
		msgFrom = txtFrom.Text.Trim();
		//"me@mycompany.com"
		msgTo = txtTo.Text.Trim();
		//"me@mycompany.com"
		msgSubject = txtSubject.Text.Trim();
		//"Your Report"
		msgBody = txtBody.Text.Trim();
		//"Attached is your report:"
		System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(msgFrom, msgTo, msgSubject, msgBody);
		mailClient.UseDefaultCredentials = true;
		mailClient.Host = "SMTPServer";
		mailClient.Port = "25";
		//Server = "http://MyServer/Root/"
		File = txtAttach.Text.Trim();
		//Server & "GenerateReport.aspx?ReportID=1"
		msg.Attachments.Add(new System.Net.Mail.Attachment(File));
		mailClient.Send(msg);
		Clear();
	} catch (Exception ex) {
		Interaction.MsgBox("Email Sending Failed");
		return;
	}
} else {
	Interaction.MsgBox("Please Enter Values");
	return;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900