C# Code for sending an email in ASP.Net
You can use following code to send an email in asp.net using C#
public bool SendMail(){
try
{
string SERVER = txtSMTP.Text;
MailMessage objMsg = new MailMessage();
int port = 25;//SMTP port number
objMsg.BodyFormat = MailFormat.Text;
objMsg.To = "abc@gmail.com";
objMsg.From = "yourmail@yourdomain";
objMsg.Cc = "xyz@gmail.com";
objMsg.Subject = "Sending E-Mails using C#";
string userName = "info@abc.com";
string password = "123";
int cdoBasic = 1;
int cdoSendUsingPort = 2;
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", SERVER);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
objMsg.Body = "This is the message body";
//If mail has an attatchment
if (FileUpload1.HasFile)
{
string filename = Server.MapPath("") + "\\userfiles\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(filename);
objMsg.Attachments.Add(new MailAttachment(filename));
}
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(objMsg);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ShowMessage("Mail Sending Fail");
return false;
}
}
0 comments:
Post a Comment