smtp by gmail

May 3, 2010 at 2:43 PM

i am facing trouble using gmails smtp with gmail account for sending mail with c#mail , but i can use it with the other private smtp's very well, what am i missing?

Thanks in advance

Coordinator
May 5, 2010 at 10:59 AM
Nothing is special with gmail as far as I know. Show me your code please. I'll investigate about it.
May 6, 2010 at 4:32 PM

Hello,

the code i tried isin below , i have changed the email address with dummy ones,

 

SmtpMessage mg = new SmtpMessage();
mg.From = email@email.com;                
mg.Date = DateTime.Now.ToUniversalTime();
mg["Mime-Version"] = "1.0";
mg.Subject = "Test" ;
mg.BodyText = "Test";

foreach (string mailadr in strMailAddrs)
{
    mg.To.Add(new MailAddress(mailadr, ""));                   
}

foreach (string astr in strAttachments)
{                    
    SmtpContent ct = new SmtpContent();
    ct.FileName = Path.GetFileName(astr);
    ct.LoadData(astr);
    mg.Contents.Add(ct);
}            

SmtpClient cl = new SmtpClient();
cl.ServerName = smtp.gmail.com;
cl.Port = 465;
cl.UserName = email@gmail.com;
cl.Password = "EMailPassword";           
//cl.HostName = "MyComputer";
cl.AuthenticateMode =SmtpAuthenticateMode.Login;
cl.Ssl = true;
SendMailResult rs = cl.SendMail(mg);

 

thanks in advance

May 11, 2010 at 4:07 AM
Edited May 11, 2010 at 4:24 AM
Hi Higty and Samael,
TQ Higty for your great work on C# mail, I tested the Higuchi.Net.Pop3.Pop3Client and have no problems using it. For the Higuchi.Net.Smtp.SmtpClient I also faced the same problem like Samael when sending through Gmail smtp.
I did managed to use the Gmail smtp if i send my email with attachment, surprisingly email without attachment will just sit in my Gmail sent items but recipient will not be able to receive them.
Well I changed 2 things to get it to working when sending email with attachment. 1) the email from address must be enclosed with the <> character for example "<xxx@abc.com>"
2) line 42 of Higuchi.Net.Smtp.SmtpCommandResultLine I added if (line!=null) to the below.
public SmtpCommandResultLine(String line)
{
if (line != null)
{
Match m = Regex.Match(line, @"(?<StatusCode>[0-9]{3})(?<HasNextLine>[\s-]{0,1})(?<Message>.*)", RegexOptions.IgnoreCase);
this._InvalidFormat = !Int32.TryParse(m.Groups["StatusCode"].Value, out this._StatusCodeNumber);
this._StatusCode = (SmtpCommandResultCode)this._StatusCodeNumber;
this._HasNextLine = m.Groups["HasNextLine"].Value == "-";
this._Message = m.Groups["Message"].Value;
}
}

The following is the sample code i used
private void button1_Click(object sender, EventArgs e)
{
SmtpMessage mg = new SmtpMessage();
mg.From = "<xxxx@abc.com>";
mg.To.Add(new Higuchi.Net.Mail.MailAddress(txtTo.Text));
mg.Date = DateTime.Now.ToUniversalTime();
mg["Mime-Version"] = "1.0";
mg.Subject = txtSubject.Text;
mg.BodyText = txtContents.Text;
foreach (object att in listBox1.Items)
{
SmtpContent cc = (SmtpContent)att;
mg.Contents.Add(cc);

}
SmtpClient cl = new SmtpClient();
cl.ServerName = "smtp.gmail.com";
cl.UserName = "xxx@gmail.com";
cl.Password = "xxx";
cl.HostName = "127.0.0.1";
cl.Ssl = true;
cl.Port = 465;
cl.AuthenticateMode = SmtpAuthenticateMode.Auto;
SendMailResult rs = cl.SendMail(mg);
if (rs.SendSuccessful == true)
{
MessageBox.Show("Mail is sent correctly.");
}
else
{
MessageBox.Show("Error");
}
}

Higty if you have time and Gmail account please investigate why email without attachment does not reach the intended recipient.
Coordinator
May 12, 2010 at 8:57 AM

Thank you for your information.
I'll investigate this problem.
Please wait few weeks.

Coordinator
May 30, 2010 at 5:22 PM

I tested below code

   SmtpMessage mg = new SmtpMessage();
   mg.From = "<higuchi@higty.com>";
   mg.To.Add(new Higuchi.Net.Mail.MailAddress("myaccount@gmail.com"));
   mg.Date = DateTime.Now.ToUniversalTime();
   mg["Mime-Version"] = "1.0";
   mg.Subject = "Attachment test!!";
   mg.BodyText = "Test";
   SmtpContent ct = new SmtpContent();
   ct.LoadData(@"C:\Mail.txt");
   mg.Contents.Add(ct);

   SmtpClient cl = new SmtpClient();
   cl.ServerName = "smtp.gmail.com";
   cl.UserName = "myaccount@gmail.com";
   cl.Password = "password";
   cl.HostName = "127.0.0.1";
   cl.Ssl = true;
   cl.Port = 465;
   cl.AuthenticateMode = SmtpAuthenticateMode.Auto;
   SendMailResult rs = cl.SendMail(mg);
   if (rs.SendSuccessful == true)
   {
    Console.Write("success");
    Console.ReadLine();
   }
   else
   {
    Console.Write("success");
    Console.ReadLine();
   }


Expected
I will receive mail with attachment at GMail account.

Observed
I receive mail with attachment at GMail account.
No problem was observed.

Hgcheong write
>email without attachment does not reach the intended recipient
Is my test code is OK?>Hgchenong

To Samael and Hgcheong.
Please send me your mail account to
higuchihiguchihiguchi@hotmail.com

I'll send you a test mail with C#Mail.

regard.

May 31, 2010 at 3:21 AM

Dear Highty

 

Yes, you are right email with attachment can be sent and received using Gmail smtp.

But email with No attachment just plain text can be sent but Not received. I just tested using the code below.

          SmtpMessage mg = new SmtpMessage();

          mg.From = "<xxxxx@gmail.com>";

           mg.To.Add(new Higuchi.Net.Mail.MailAddress(xxxx@yahoo.com));

           mg.Date = DateTime.Now.ToUniversalTime();

           mg["Mime-Version"] = "1.0";

           mg.Subject = "No Attachment test!!";

           mg.BodyText = "Test";

           // SmtpContent ct = new SmtpContent();

           // ct.LoadData(@"C:\Mail.txt");

           // mg.Contents.Add(ct);

 

           Higuchi.Net.Smtp.SmtpClient cl = new Higuchi.Net.Smtp.SmtpClient();

           cl.ServerName = "smtp.gmail.com";

           cl.UserName = xxxxx@gmail.com;

           cl.Password = "xxxxxxxx";

           cl.HostName = "127.0.0.1";

           cl.Ssl = true;

           cl.Port = 465;

           cl.AuthenticateMode = SmtpAuthenticateMode.Auto;

           SendMailResult rs = cl.SendMail(mg);

           if (rs.SendSuccessful == true)

           {

               Console.Write("success");

               Console.ReadLine();

           }

           else

          {

               Console.Write("Not successful");

               Console.ReadLine();

           }

Jun 2, 2010 at 3:42 AM

Dear Higty,

Please ignore my previous post, I can receive all the email with and without attachment with no issue today using gmail smtp. Thanks for testing.

Coordinator
Jun 5, 2010 at 1:41 PM

OK.
If you find some problem in the future, please tell me it.
Thank you.