How to send EMAIL from WINDOW APPLICATION..

Posted by Sp under ASP.NET on 12/4/2010 | Points: 10 | Views : 2276 | Status : [Member] | Replies : 5
How to send EMAIL from WINDOW APPLICATION..




Responses

Posted by: Sivaprasadadiraju on: 12/4/2010 [Member] Starter | Points: 25

Up
0
Down
Hi,

use following method to send from windows or web with attachment

public void SendMailToWithAttachment(string To, string MessageBody, string Subject, string From, string CC, string Attachments)
{

int iSmtpPort = 25;
string strTo = To;
string strFrom = From;
string strSubject = Subject;
string strBody = MessageBody;
string strCC = CC;
string strAttachments = Attachments;

MailMessage oMailMessage = new MailMessage(strFrom, strTo, strSubject, strBody);
oMailMessage.CC.Add(new MailAddress(strCC));
Attachment myAttachment = new Attachment(strAttachments);
oMailMessage.Attachments.Add(myAttachment);
oMailMessage.IsBodyHtml = true;
string SMTPServer = string.Empty;
SMTPServer = ConfigurationManager.AppSettings["smtpServer"].ToString();
SmtpClient oSmtpClient = new SmtpClient(SMTPServer, iSmtpPort);

// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
CredentialCache.DefaultNetworkCredentials.GetCredential(SMTPServer, 25, "1");
CredentialCache.DefaultNetworkCredentials.Password = ConfigurationManager.AppSettings["sendusername"].ToString();
CredentialCache.DefaultNetworkCredentials.UserName = ConfigurationManager.AppSettings["sendpassword"].ToString();
//client.UseDefaultCredentials = false;
oSmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;

try
{
oSmtpClient.Send(oMailMessage);
}
catch (Exception oException)
{
string strDummy = oException.Message;
//throw new Exception("Exception caught in Sending mail(1)" + oException.Message);
}
finally
{
oMailMessage.Attachments.Dispose();
}
}


SIVA PRASAD ADIRAJU

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/4/2010 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Sending an email does not matter if you are on windows or Web.

Look at this
http://www.dotnetfunda.com/articles/article747-how-to-send-an-email-using-gmail-in-aspnet-.aspx



Thank you for posting at Dotnetfunda
[Administrator]

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sp on: 12/4/2010 [Member] Starter | Points: 25

Up
0
Down
I have written this code on button click:
System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("jtrj@mailinator.com", "jtrj@mailinator.com",
"This is the mail subject", "Just wanted to say Hello");

MyMailMessage.IsBodyHtml = false;

//Proper Authentication Details need to be passed when sending email from gmail
System.Net.NetworkCredential mailAuthentication = new
System.Net.NetworkCredential("jtjtjtrj", "trjjt");

//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
//For different server like yahoo this details changes and you can
//get it from respective server.
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 465);

//Enable SSL
mailClient.EnableSsl = true;

mailClient.UseDefaultCredentials = false;

mailClient.Credentials = mailAuthentication;

mailClient.Send(MyMailMessage);
MessageBox.Show("Email sent");

I get {"Unable to connect to the remote server"} error

SocketException (0x2743)



Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sivaprasadadiraju on: 12/4/2010 [Member] Starter | Points: 25

Up
0
Down
MailMessage mail = new MailMessage();


mail.To.Add("to@gmail.com");

mail.From = new MailAddress("from@gmail.com");

mail.Subject = "Test Email";

string Body = "<b>Welcome to CodeDigest.Com!!</b>";

mail.Body = Body;

mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();

smtp.Host = ConfigurationManager.AppSettings["SMTP"];

smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);

smtp.EnableSsl = true;

smtp.Send(mail);



Web.Config

<appSettings>

<add key="SMTP" value="smtp.gmail.com"/>

<add key="FROMEMAIL" value="mail@gmail.com"/>

<add key="FROMPWD" value="password"/>

</appSettings>



Regards,
Siva Prasad Adiraju


SIVA PRASAD ADIRAJU

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sivaprasadadiraju on: 12/4/2010 [Member] Starter | Points: 25

Up
0
Down
Here you should use Gmail accounts only...Check once with your firewalls and antivirus settings sometimes it may stop to send mails

Regards,
Siva Prasad Adiraju

SIVA PRASAD ADIRAJU

Sp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response