Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to implement a simple mailing functionality into my application using the following code But it gives some Error while is invoke Send method on SmtpMail obj ..
Please help ...!!

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage oMsg = new MailMessage();
                
                oMsg.From = "Mee@yahoo.in";
                
                oMsg.To = "You@hotmail.com";
                oMsg.Subject = "Send Using Test Mail";
                
                oMsg.BodyFormat = MailFormat.Text;

                string bodyText = textBox1.Text; // this textbox is part of the Application UI

                
                oMsg.Body = " Hey !!! This is meeee .. \n Sending this mail via Test Mail Application.. \n Cheers !!" + "\n " + bodyText;

                // ADD AN ATTACHMENT.
                // Path to attachment.
                String sFile = @"C:\Users\LUCKY\Desktop\Hello.txt";
                MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);

                oMsg.Attachments.Add(oAttch);

               
                SmtpMail.SmtpServer = "localhost";// "smtp.gmail.com";
                SmtpMail.Send(oMsg);

                oMsg = null;
                oAttch = null;
            }
            catch (Exception ex)
            {
                //Console.WriteLine("{0} Exception caught.", ex);
                MessageBox.Show("Exception caught." +  ex.Message);
            }
        }


the ERROR says : The Transport failed to connect to server.
I have IIS installed and i Have Windows7 as OS. IDE : VS 2010 .

Additional Question : There is no way we can have "From" field different for different users of this application which has my single GMAIL SMTP Gateway .?? Is there any way out ???


Thanks in Advance !!
Posted
Updated 26-May-11 7:37am
v4

There is a simple but effective option to send mails through a class library (.dll file) using GMAIL SMTP Server.

The dll enables you to send mails through your GMAIL account to any other SMTP servers in a fraction of seconds with attachments.

Codeproject doesnt allow me to upload a file. So, you can mail me at manjumysore.k@gmail.com so I could send you the dll file.

All the best.
 
Share this answer
 
Comments
XtremLucky 26-May-11 4:16am    
Sir,
I got ur point ..
Actually I had one suggestion which worked perfectly fine for mee and that is via GMAIL SMTP Server .. But just incase im unaware . Plz can u let mee know:


Is it possible for a person to send an email from a c# code using the resources of his/her local pc. For instance the code that works fine for mee uses the Gmail account id and pass but
Is is possible to send a mail independent of any external SMTP server ?

HOpe im clear in my question ..
I have mailed u for recieving ur suggested code .

Thanks for ur help ,Sir
manjukgowda 11-Jun-13 8:23am    
using System.Web.Mail;

public static bool SendEmail(string EmailId, string Password, string To, string Subject, string Body, string AttachmentPath)
{
EmailId = EmailId.EndsWith("yahoo.in") ? EmailId.Replace("yahoo.in", "yahoo.com") : EmailId;
string server = null;
string port = null;
if (EmailId.Contains("gmail"))
{
server = "smtp.gmail.com";
port = "465";
}
else if (EmailId.Contains("yahoo"))
{
server = "smtp.mail.yahoo.com";
port = "465";
}
else if (EmailId.Contains("hotmail"))
{
server = "smtp.live.com";
port = "25";
}
else
{
server = "smtp.aol.com";
port = "465";
}
try
{
MailMessage myMail = new MailMessage();
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserver",
server);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
port);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusing",
"2");

myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//Use 0 for anonymous
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusername",
EmailId);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendpassword",
Password);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
"true");
myMail.Priority = MailPriority.High;

myMail.From = EmailId;
myMail.To = To;
myMail.Subject = Subject;
myMail.BodyFormat = System.Web.Mail.MailFormat.Html;
myMail.Body = Body;
if (AttachmentPath.Trim() != "")
{
MailAttachment MyAttachment = new MailAttachment(AttachmentPath);
myMail.Attachments.Add(MyAttachment);
}
SmtpMail.SmtpServer = string.Format("{0}:{1}", server, port);
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
}

Your application cannot connect to the mail server specified

If SmtpMail.SmtpServer points to "localhost" (or the equivalent), be sure the SMTP Service is running on port 25.
 
Share this answer
 
Comments
XtremLucky 26-May-11 3:42am    
Sir ,
How may i know whether the Smtp Service is running or not ..
I have already started the the SMTP Service available in the IIS with "localhost" as SMTPSERVER and 25 as port no

Thanks
Dalek Dave 26-May-11 3:58am    
Well said.
check out this link

http://www.codeproject.com/Questions/198066/error-in-smtp-sending-mail-reg.aspx

http://www.codeproject.com/Questions/198356/sending-of-auto-response-mails-reg.aspx


regards,
gowtham
 
Share this answer
 
v5
Comments
Dalek Dave 26-May-11 3:58am    
Good old Archive to the rescue again!
Actually This Works :

try
  {
     SmtpClient mailClient;

     mailClient = new SmtpClient("smtp.gmail.com", 587);
     //mailClient.Credentials = new NetworkCredential("MyGmailEmail@gmail.com", "MyGmailPassword");
                
     //enable ssl
     mailClient.EnableSsl = true;

     // Create the mail message
     System.Net.Mail.MailMessage mailMessage = new MailMessage();//(from, to, subject, body);
                
     mailMessage.From = new MailAddress("FROMADDRESS@gmail.com");
     mailMessage.To.Add(to);
     //mailMessage.Attachments
     string fileLoc = @"C:\Users\LUCKY\Desktop\Hello.txt";
     //Attachment attachment = new Attachment(fileLoc);
     mailMessage.Subject = subject;
     mailMessage.Body = body;
     mailMessage.IsBodyHtml = isBodyHtml;
     mailMessage.Priority = mailPriority;

     mailClient.Send(mailMessage);

     return true;
   }
   catch (Exception ex)
   {   
     return false;
   }

This is works fine , but requires gmail account (id & pwd) for sending mail using Gmail SMTP Server.
WHat if i don't wanna use Gmail's Server and instead use the one provided by Windows 7 as localhost ..

Best Regards
Thanks .
-Live Lucky :)Ω
 
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