I'm struggling with sending mail with c#.
This is the code I found on the web.
I don't have a server so I think I can't use something in this code.
I'm not familiar with the mail syntax smtp etc.
I figured I could do this the easy way.
Create a webmail account such as gmail, give the password and username and use this.
But this code doesn't work like this.
If you know a working snippet then please refer me to it.
"; mail.Body = Body; |
Thank You
Bryian TanPosted Nov 26, 2010, 11:25 PM
This code snippet will help.
protected void Page_Load(object sender, EventArgs e)
{
if (SendEmail())
Response.Write("success");
else
Response.Write("problem");
}
public bool SendEmail()
{
bool isSuccess = false;
string from = "[email protected]"; //Replace this with your own correct Gmail Address
string to = "[email protected]"; //Replace this with the Email Address to whom you want to send the mail
string cc = "[email protected]";
string bcc = "";
string fromName = "bryian";
string fromEmail = "[email protected]";
string userName = "[email protected]";
string password = "YourGmailPassword";
Attachment attachment = null;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.Port = 587; // Gmail works on this port
client.EnableSsl = true; //Gmail works on Server Secured Layer
client.Host = "smtp.gmail.com";
try
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(from, fromName);
mail.ReplyTo = new MailAddress(from, fromName);
mail.Sender = mail.ReplyTo;
if (to.Contains(";"))
{
string[] _EmailsTO = to.Split(";".ToCharArray());
for (int i = 0; i < _EmailsTO.Length; i++)
{
mail.To.Add(new MailAddress(_EmailsTO[i]));
}
}
else
{
if (!to.Equals(string.Empty))
{
mail.To.Add(new MailAddress(to));
}
}
if (cc.Contains(";"))
{
string[] _EmailsCC = cc.Split(";".ToCharArray());
for (int i = 0; i < _EmailsCC.Length; i++)
{
mail.CC.Add(new MailAddress(_EmailsCC[i]));
}
}
else
{
if (!cc.Equals(string.Empty))
{
mail.CC.Add(new MailAddress(cc));
}
}
if (bcc.Contains(";"))
{
string[] _EmailsBCC = bcc.Split(";".ToCharArray());
for (int i = 0; i < _EmailsBCC.Length; i++)
{
mail.Bcc.Add(new MailAddress(_EmailsBCC[i]));
}
}
else
{
if (!bcc.Equals(string.Empty))
{
mail.Bcc.Add(new MailAddress(cc));
}
}
string message = "this is a test message";
mail.Subject = "test subject";
mail.Body = "
mail.Body += message.Replace("\n", "
") + "
";
mail.Body += "
";
mail.Body += "
Author information
";mail.Body += "
mail.Body += "Name: " + fromName + "
";
mail.Body += "E-mail: " + fromEmail + "
";
mail.IsBodyHtml = true;
if (HttpContext.Current != null)
{
mail.Body += "IP address: " + HttpContext.Current.Request.UserHostAddress + "
";
mail.Body += "User-agent: " + HttpContext.Current.Request.UserAgent;
}
if (attachment != null)
{
mail.Attachments.Add(attachment);
}
client.Send(mail);
isSuccess = true;
}
}
catch (Exception ex)
{
isSuccess = false;
} // end try
return isSuccess;
}
CrishPosted Nov 29, 2010, 7:10 AM
i have tried it works in my end.