Our application has the possibility to send emails via SmtpClient. One of our customer is trying to do so by using his gmail account and it fails resulting in a timeout. However, from our testing lab it works just fine (with another gmail account).
In the trace i can see that the server is not answering with Certificate, Server Key Exchange, Server Hello Done. And im wondering what can be the cause for this?
I also noticed in the traces, the customer is using TLSv1 so I tried to replicate the error on a Windows7 system but still it works for me.
oSmtp = new SmtpClient(this.host, this.port);
oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
oSmtp.EnableSsl = ssl;
NetworkCredential oCredential = new NetworkCredential(this.userName, this.password);
oSmtp.Credentials = oCredential;
oSmtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userState = null;
oSmtp.SendAsync(oMsg, userState);
As far as the code goes, enableSsl is true, the port is 587 and we also instructed our customer to check his gmail account and allow less secure applications.
We will ask the customer for more specific details and try to put more traces in our application, but i would like to know if there is anything that can prevent the server to answer with Certificate,...
Inspecting the traces revealed no significant difference between customers Client Hello and our test Client Hello.
Thanks!
This is a working sample i used few days ago:
string fromAddress = "fromaddress#gmail.com";
var toAddress = new MailAddress("fake#email.com", "To person");
const string fromPassword = "pass";
const string subject = "test";
const string body = "Hey now!!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(fromAddress);
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
//mail.Attachments.Add(new Attachment("C:\\file.zip"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
also make sure to enable Less secure app access for the gmail you are using to send data:
Related
I am working on an online shopping website. I want the users directly contact each other via emails by using a web form. I have tried different ways. but it didn't work.
I was also trying to use the gmail smtp as follow:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("example#gmail.com", "mypassword");
client.EnableSsl = true;
string fromAddress = "buyer#gmail.com";
string messageSubject = "Your Subject";
string body = "Content";
var supEmail = "seller#yahoo.com";
MailMessage mm = new MailMessage(fromAddress, supEmail, messageSubject, body);
MailAddress copy = new MailAddress("notifications#mydomain.com");
mm.CC.Add(copy);
//Set up your encoding
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.Priority = System.Net.Mail.MailPriority.High;
//Send your message
client.Send(mm);
Any idea or suggestions?
This won't work. Google won't allow you to send an email from an account you don't own... so if you want to send an email via Google then it needs to be from either the Google email account your are validating with or associated with that account.
I see many posts on SMTPClient but I'm still stuck.
I am creating a C# Console application targeting the .NET 4.0 framework and running on a Windows 2008 R2 Datacenter (server).
I want send an email using SSL from a gmail account I just created.
If I use port 587, I get an exception:
The SMTP server requires a secure connection or the client was not authenticated
If I use port 465, I get another one:
Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.
I realize this question has been asked several times in the past. All answers I see relate to the order in which UseDefaultCredentials is set. It must be set to false BEFORE the real credentials are set. I have followed this advice (as per the code below) and yet I still get a failure.
Other answers include switching ports from 465 to 587... which i have tried and just results in a different error (as stated above).
Here's my code:
NetworkCredential credentials = new NetworkCredential("something#gmail.com", "2-step-auth-Password");
MailAddress address = new MailAddress("something#gmail.com");
SmtpClient client = new SmtpClient("smtp.gmail.com", 465); // or 587
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = credentials;
client.Timeout = 10000; // 10 seconds.
client.Host = "smtp.gmail.com";
//client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
MailMessage message = new MailMessage("example#gmail.com", recipients);
message.Subject = "Hello";
message.Body = "Goodbye";
message.BodyEncoding = Encoding.UTF8;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString());
}
message.Dispose();
Update 2: I see another answer which may be my issue: gmail requires an application-specific password (which is apparently not available with my current gmail account. not sure why yet). but this may be my issue.
Update 3: I have followed all instructions. Generated 2-step authentication with Gmail, and still fails.
Below code works for me, you can try this:
SmtpClient smtp = new SmtpClient("smtp.office365.com");
//Your Port gets set to what you found in the "POP, IMAP, and SMTP access" section from Web Outlook
smtp.Port = 587;
//Set EnableSsl to true so you can connect via TLS
smtp.EnableSsl = true;
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("Username", "Password");
smtp.Credentials = cred;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Sender Email Address", "Sender Name");
StringBuilder mailBody = new StringBuilder();
mail.To.Add("ToEmailAddress");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "Body Text";
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
I am developing a program which needs to have the capability to send emails. I've got a simple mail function setup however I have never really delved into the mail side of things and am not sure if I'm using the correct settings.
I am sure I am doing something wrong with the SMTP, I have set the MailMessage host as the outgoing mail server that I use for emailing from outlook (the email accounts are hosted on shared virtual hosting so I use their supplied hostname in the function) alongside the login credentials I would normally use. When I try to send a test email it throws an unable to connect to remote server exception. I have WAMPSERVER setup on the computer I am trying to run this from, I know it has some kind of SMTP capability? Should I be using this or is there no reason I can't use shared virtual hosting SMTP as the host? Please refer to code below-
public void EmailTracking()
{
string to = "johnsmith#xxxxxxxxxxxxx.com.au";
string body = "this is some text for body";
string subject = "subject line";
string fromAddress = "kelvie#xxxxxxxxxx.com.au";
string fromDisplay = "Kelvie";
string credentialUser = "removed";
string credentialPassword = "removed";
string host = "removed";
MailMessage mail = new MailMessage();
mail.Body = body;
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(to));
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
mail.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
smtp.Host = host;
smtp.Send(mail); //fails here with unable to connect to remote server
}
I've been struggeling a lot with this now.
I try to send mail with my mvc application using my google apps account. But I keep getting errors. It doesn't matter which settings I use. I tried using both port 465 and 587 with ssl and authentication turned on. With 465 I get Operation Timed Out and with 587 I get this message:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required
I tried turning of the firewall with no luck. I have also tried to turn off 2-step authentication but I figured out that it wasn't even turned on.
I hope that you can help me
Regards
Here is the code as requested:
public static void SendMail(MailAddress from, MailAddress to, string subject, string body) {
MailMessage mail = new MailMessage();
mail.From = from;
mail.To.Add(to);
mail.Subject = subject;
mail.Body = body;
SmtpClient client;
if (Settings.Port != null)
client = new SmtpClient(Settings.Host, Int32.Parse(Settings.Port));
else
client = new SmtpClient(Settings.Host);
client.EnableSsl = Settings.UseSSL;
client.Timeout = 50000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
if (Settings.UseAuthentication) {
client.Credentials = new NetworkCredential(Settings.Username, Settings.Password);
client.UseDefaultCredentials = false;
}
client.Send(mail);
}
I have checked all obvius stuff like username and password (username is formed like user#domain.com). I've also stepped through the code to verify my settings class is working as it should
I found out what the problem was. client.UseDefaultCredentials = false; must go before the credentials is set. Now client.Sendmethod returns without exceptions.
I am trying to send an email using the following very standard code. However, I get the error that follow...
MailMessage message = new MailMessage();
message.Sender = new MailAddress("sen#der.com");
message.To.Add("reci#pient.com");
message.Subject = "test subject";
message.Body = "test body";
SmtpClient client = new SmtpClient();
client.Host = "mail.myhost.com";
//client.Port = 587;
NetworkCredential cred = new NetworkCredential();
cred.UserName = "sen#der.com";
cred.Password = "correct password";
cred.Domain = "mail.myhost.com";
client.Credentials = cred;
client.UseDefaultCredentials = false;
client.Send(message);
Mailbox unavailable. The server
response was: No such
user here.
This recipient email address definitely works. To make this account work I had to do some special steps in outlook. Specifically, I had to do change account settings -> more settings -> outgoing server -> my outgoing server requires authentication & use same settings. I am wondering if there is some other strategy.
I think the key here is that my host is Server Intellect and I know that some people on here use them so hopefully someone else has been able to get through this. I did talk to support but they said with coding issues I am on my own :o
try this...
NetworkCredential cred = new NetworkCredential();
cred.UserName = "sen#der.com";
cred.Password = "correct password";
//cred.Domain = "mail.myhost.com";
... you should not need to provide the .Domain unless you are using Kerberos or some other complex authentication.
Edit...
Check out my extended answer. It has an example of how to send an email with authentication. It's also has SSL enabled so you may need to remove that part.
there is no mailbox called sen#der.com on server mail.myhost.com, check that