I am using below code for sending email notification
private static void EMail() {
MailMessage message = new MailMessage();
message.From = new MailAddress(SourceEmail);
message.To.Add(Tolist);
message.CC.Add(CClist);
message.Subject = Subject;
message.IsBodyHtml = true;
message.Body = BODY;
using(SmtpClient smtpClient = new SmtpClient()) {
smtpClient.EnableSsl => true;
smtpClient.UseDefaultCredentials = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
smtpClient.Credentials = (ICredentialsByHost) new NetworkCredential(U_Name, PWD);
smtpClient.Host = Host;
smtpClient.Port = Port;
smtpClient.DeliveryMehod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
Environment.Exit(0);
}
}
But it was failing with below error, Please help me here
Information: Error while Sending Email at
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,
String response) at System.Net.Mail.MailCommand.Send(SmtpConnection
conn, Byte[] command, MailAddress from, Boolean allowUnicode)your
text at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,
MailAddressCollection recipients, String deliveryNotify, Boolean
allowUnicode, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message) at
Program.EMail() at Program.Main(String[] args)
I would expect to send email or show some valid error to debug further
Related
I can only send mail within same mail-domain, but not outside it. Am I missing something?
try {
MailMessage mail = new MailMessage("noreply#company.com", "user#mail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.company.com";
mail.Subject = "Thanks...";
mail.Body = "text...";
client.Send(mail);
} catch (Exception ex) {
Response.Write(ex.ToString());
}
Error message when sending to other mail-domain:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The
server response was: 5.7.1 Unable to relay at
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,
MailAddressCollection recipients, String deliveryNotify, Boolean
allowUnicode, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message)
I've no control over the smtp host, but I should be able to send to other mail-domain.
I am developing one Windows Application sending bulk mails.
It worked successfully for 100 mails. After that it starts showing
these errors
The server response was: 5.7.0 Too many messages in too short a time frame at
System.Net.Mail.SmtpTransport.SendMail(
MailAddress sender,
MailAddressCollection recipients,
String deliveryNotify,
Boolean allowUnicode,
SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at SmsAndEmail.Email.EmailIndex.SendTheMail()
This is my code:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("MailAddress");
msg.To.Add(mail.ToAddress);
msg.Subject = "MySub";
msg.Body = mail.EmailBody;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "hostname";
client.Port = 587;
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("MailAddress", "password");
client.Timeout = 20000;
ServicePointManager.ServerCertificateValidationCallback =
delegate(
object s,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)
{ return true; }
;
try
{
client.Send(msg);
newStatus = "SUCCESS";
}
catch (Exception ex)
{
newStatus = "Failed";
errorMessage = "Mail Sending Failed !";
return errorMessage;
}
I am using below code in c#,
string Subject = "test12";
MailMessage mail = new MailMessage();
mail.To.Add(item.ToString());
mail.From = new MailAddress(EmailUserName);
mail.Subject = Subject;
mail.Body = PopulateBody();
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient(EmailHost, EmailPort);
client.EnableSsl = true;
NetworkCredential credentials = new NetworkCredential(EmailUserName, EmailPassword);
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.Send(mail);
I am getting error in Client.send(mail) method
What I have tried:
System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The function requested is not supported
--- End of inner exception stack trace ---
at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
at
I would try without authentication first to check if that gives a different error and also try without ssl.
protected bool NotifyByMail(string server, string strFrom, string strTo, string strSubject, string strBodyText, bool isBodyTextHtml = false)
{
if (string.IsNullOrEmpty (server)
|| string.IsNullOrEmpty (strFrom)
|| string.IsNullOrEmpty (strTo)
|| string.IsNullOrEmpty (strSubject)
|| string.IsNullOrEmpty (strBodyText))
return false;
try {
MailAddress from = new MailAddress (strFrom);
MailAddress to = new MailAddress (strTo);
MailMessage message = new MailMessage (from, to);
message.Subject = strSubject;
message.Body = strBodyText;
message.IsBodyHtml = isBodyTextHtml;
SmtpClient client = new SmtpClient (server);
// Include credentials if the server requires them.
//client.Credentials = new System.Net.NetworkCredential ("********", "*******");// System.Net.CredentialCache.DefaultNetworkCredentials;
client.Send (message);
return true;
}
catch (Exception exception) {
// TODO ErrorHandling
}
return false;
}
Bellow is my code when i remove DisplayName Property of MailAddress() it work fine but Receiving end mail show EmailID on Display Name like emailID#gmail.com.
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("emailID#domainName.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient();
smtp.Port = 587; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential(mail.From.ToString(), "password"); // [4] Added this. Note, first parameter is NOT string.
smtp.Host = "smtp.gmail.com";
mail.IsBodyHtml = true;
mail.Subject = Subject;
mail.Body = Body;
mail.To.Add(new MailAddress(To));
smtp.Send(mail);
mail.Dispose();
When i add Display name of MailAddress() i receive this error message.
System.Net.Mail.SmtpException: The SMTP server requires a secure
connection or the client was not authenticated. The server response
was: 5.5.1 Authentication Required. Learn more at at
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,
String response) at System.Net.Mail.SmtpTransport.SendMail(MailAddress
sender, MailAddressCollection recipients, String deliveryNotify,
Boolean allowUnicode, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message) at ***
Where i am wrong?
var client = new SmtpClient
{
Host = "smtp.googlemail.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
Credentials = new NetworkCredential(SmtpUserName, SmtpUserPassword);
};
client.Send(mail);
where mail is an instance of MailMessage
private static SecureString _smtpPassword;
public static SecureString SmtpUserPassword
{
get
{
if (_smtpPassword != null)
return _smtpPassword;
_smtpPassword = new SecureString();
foreach (var c in "your password here")
_smtpPassword.AppendChar(c);
_smtpPassword.MakeReadOnly();
return _smtpPassword;
}
}
I can't send a email message using gmail settings. i already tried client.Host ="localhost" it's working but not in client.Host ="smtp.gmail.com".. Please help me guys.. I need use client.Host ="smtp.gmail.com".. thanks
here's my C# code:
string from = "aevalencia119#gmail.com"; //Replace this with your own correct Gmail Address
string to = "aevalencia191#gmail.com"; //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to); mail.From = new
MailAddress(from, "One Ghost" ,System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ; mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient(); //Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "iseedeadpoeple");
client.Port = 587; // Gmail works on this port client.Host ="smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
} HttpContext.Current.Response.Write(errorMessage
);
} // end try
here's the error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Much thanks guys!
You need to get and send mail to GMail by using SSL secutiry certificate
MailMessage msgMail = new MailMessage("a#gmail.com", "b#mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a#gmail.com", "a");
try
{
smtp.Send(msgMail);
}
catch (Exception ex)
{
}
reference: http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4
public static string sendMail(string to, string title, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
if (to == "")
to = "aevalencia119#gmail.com";
MailAddressCollection m = new MailAddressCollection();
m.Add(to);
mail.Subject = subject;
mail.From = new MailAddress( "aevalencia119#gmail.com");
mail.Body = body;
mail.IsBodyHtml = true;
mail.ReplyTo = new MailAddress("aevalencia119#gmail.com");
mail.To.Add(m[0]);
smtp.Host = "smtp.gmail.com";
client.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("aevalencia119#gmail.com", "####");
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mail);
return "done";
}
catch (Exception ex)
{
return ex.Message;
}
}