I've got the error "Relay access denied", but I can connect my account with some email programs.
My code:
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("xx#xx.com", "xxx");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("xx#xx.com");
smtpClient.Host = "mail.xx.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
message.To.Add("aa#aa.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Relay access denied...
}
Does anyone know the reason for this?
Since your smtp host port is 587, I think you should set smtpClient.EnableSsl to true before calling its Send method.
Although this question is almost a year old now, I just stumbled upon the solution to a similar problem today. Apparently, the System.Net.Mail.SmtpClient class does not support the AUTH PLAIN method. Unfortunately, the SMTP server I was trying to use only offered AUTH PLAIN. Since the SmtpClient class and my server couldn't agree on an authentication method, the SmtpClient tried to send mails without authentication which got rejected by the SMTP server...
You should make sure that the SMTP server you are using offers at least one of the authentication mechanisms that SmtpClient supports. I couldn't find an exhaustive list of supported mechanisms but my server is now offering AUTH LOGIN which works with SmtpClient.
Please try
SmtpClient client = new SmtpClient("mail.xx#xx.com");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xx#xx.com", "xxx");
client.Port = 587;
client.EnableSsl = true;
MailMessage maili = new MailMessage();
maili.Body = body;
maili.Subject = subject;
maili.IsBodyHtml = true;
maili.From = new MailAddress("xx#xx.com");
maili.To.Add("aa#aa.com");
try
{
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
client.Send(maili);
}
catch (Exception ex)
{
throw ex;
}
maili.Dispose();
In my case, the smtp password had changed, and gave me this error...
Usually it's very simple: the "From" e-mail address must have the same domain name as the domain name of the account from which the e-mail is being sent. So, if you send e-mail through a Hotmail account with the e-mail address e.g. "john.doe#hotmail.com", the "From" address must be set to the same.
Related
So, this is my email function, that sends an email using Gmail's SMTP:
public void Email(string subject, string body)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(email, emailname);
message.To.Add(new MailAddress(targetemail, targetname));
message.Subject = subject;
smtp.EnableSsl = true;
message.IsBodyHtml = true;
message.Attachments.Add(new Attachment(folder + #"\" + cbPrezentacja.SelectedItem.ToString() + "." + extension));
message.Body = body;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(email, password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
It works however, how would I schedule the email for a specific time? I don't mean making a Windows schedule/timer on the host (that requires leaving it on) or whatever, I want to do it via Gmail.
https://i.stack.imgur.com/eNG9D.png < This is how it works using Gmail. (sorry for the weird tint)
Is this even possible using System.Net.Mail or do I need a special Gmail API for that (if so, how?)? This is a private application for 1 computer, so it doesn't need to be super secure.
Thanks!
You can't.
There's nothing in SMTP to schedule it, when you do smtp.Send(message); the message is sent that exact moment.
Gmail has an API, but (as far I can see) it doesn't offer this functionality, so right now the only way to do it would be exactly what you say you don't want: your app should somehow wait till the desired time and send it.
I'm trying to send an email via .net smtp. I am receiving this message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("server", 587);
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.EnableSsl = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
mail.From = new MailAddress(FromMail);
mail.To.Add("emailaddress");
mail.Bcc.Add("emailaddress");
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpServer.Send(mail);
The issue I believe is that the servers are located in the DMZ...anyone have any insight on how to fix this? If we assign the user with domain admin rights, the emails work...due to security reasons we don't want to go that route.
Issue resolved...the network team didn't have port 587 available...once they added it, emails started working for anonymous sender. I no longer need to use credentials. Thanks to all who replied.
So im trying to send an email on C# and always getting the same error: "Additional information: The SMTP server requires a secure connection or the client was not authenticated." i have checked out pretty much every single link i could find about this i have diabled all gmail protection and my credentials are correct i don't know why this error continues
MailMessage mail = new MailMessage();
mail.From = new MailAddress("xx#gmail.com");
mail.Sender = new MailAddress("xx#gmail.com");
mail.To.Add("external#emailaddress");
mail.IsBodyHtml = true;
mail.Subject = "Email Sent";
mail.Body = "Body content from";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("xx#gmail.com", "xx");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Timeout = 30000;
try
{
smtp.Send(mail);
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message.ToString());
}
This is the exception message from C#, what is the actual response from the server? You may need to enable "less secure apps" to connect to your gmail account first (see https://www.google.com/settings/security/lesssecureapps).
I have tried your code getting the error "5.5.1 Authentication required". Please give this link a try Sending email through Gmail SMTP server with C#
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'm trying out very simple tutorial on how to send e-mail via .NET and C#. However when I try to execute the code I got the following exception:
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The operation has timed
out.
So what I have done is to find the server settings to which I want to send mails to. This is very popular in my country:
Incoming settings
Protocol -> POP
Email address -> username#abv.bg
Username -> username#abv.bg
Password -> password
POP server -> pop3.abv.bg
Security type -> SSL
Server port -> 995
Outgoing server settings
Username -> username#abv.bg
Password -> password
SMTP server -> smtp.abv.bg
Security type -> SSL
Server port -> 465
then I created a Console project and in my main method I have this code:
SmtpClient client = new SmtpClient("smtp.abv.bg");
client.Port = 465;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(
"username#abv.bg", "password");
MailMessage msg = new MailMessage();
msg.To.Add("username#abv.bg");
msg.From = new MailAddress("username#abv.bg");
msg.Subject = "Test subject";
msg.Body = "Test test test...";
client.Send(msg);
Since I have no experience with this I just try the code as you see it. No additional settings anywhere. The only thing that I think should not be problem but I think worth mentioning that here:
client.Credentials = new NetworkCredential(
"username#abv.bg", "password");
and here:
msg.To.Add("username#abv.bg");
I'm using the same e-mail. But I think this shouldn't be a problem. Any idea what am I doing wrong here?
Here is a working example (with Gmail) I wrote years ago for testing.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx#gmail.com");
mail.From = new MailAddress("yyy#gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy#gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}
I would try this function with Gmail just to eliminate network related issues like firewall. When that will work, the rest is just to find the right settigs for the SMPT server