I've recently created an web application in C# where I send emails to one mailbox. Everything works fine for the first 10 to 15 emails. After that, it gives the error :
The Mailbox unavailable. The server response was: 5.7.3 Requested
action aborted; user not authenticated
I then have to authenticate my email address again in order for it to work.
I use outlook/hotmail to send the emails.
Has any one ever had a similar issue?
Code Example
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("email#outlook.com");
mail.To.Add("thisemail#address.com");
mail.Subject = "test1";
mail.IsBodyHtml = true;
mail.Body = "";
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("address#outlook.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Response.Write("success");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Related
I have a web server at home running IIS 10 and .Net 4.8.
I am trying to send an email through a C#.Net, using the following yahoo stmp service call.
However, I cannot seem to get it to work? Whenever I try to execute the following code, the web page seems to be loading for about 30 seconds, then returns an "SMTP Server returned an invalid response" error message, which apparently doesn't mean anything specific? This is getting pretty frustrating as I've been on this for over 4 hours now... so thanks for any help!
using (MailMessage mm = new MailMessage("uneviesystems#yahoo.com", "MaxOvrdrv007#yahoo.ca"))
{
mm.Subject = "test";
mm.Body = "testing maudine...";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.mail.yahoo.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("uneviesystems#yahoo.com", "*******");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 465;
try
{
smtp.Send(mm);
}
catch(SmtpException ex)
{
string p = "";
}
}
Port 465 isn't supported by System.Net.Mail.SmtpClient -
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx
You could try using port 587 instead (if Yahoo supports it) and disable SSL.
try using application password instead account password. You can generate application password in account settings.
Send your email asynchronously and format your code to dispose client properly.
using (var message = new MailMessage())
{
message.To.Add(new MailAddress("recepient email", "receipient name"));
message.From = new MailAddress("your email", "your name");
message.Subject = "My subject";
message.Body = "My message";
message.IsBodyHtml = false; // change to true if body msg is in html
using (var client = new SmtpClient("smtp.mail.yahoo.com"))
{
client.UseDefaultCredentials = false;
client.Port = 587;
client.Credentials = new NetworkCredential("your email", "your password");
client.EnableSsl = true;
try
{
await client.SendMailAsync(message); // Email sent
}
catch (Exception e)
{
// Email not sent, log exception
}
}
}
My application have function to sent email trough smtp.gmail.com
It works well when i run at local, but after i publish to vps, the email not sent.
dns setting for mx(10) was pointing to smtp.gmail.com
here's the code that i use :
public ServiceResult SendEmailOrder(string Email)
{
ServiceResult result = new ServiceResult();
try
{
const string fromPassword = "password";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("senderaddress#gmail.com");
mail.Subject = "subject mail";
mail.Body = "Body Mail";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("senderaddress#gmail.com", fromPassword);
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail.To.Add(new MailAddress(Email));
SmtpServer.Send(mail);
}
catch (Exception ex)
{
result.Error("Failed Send Email", ex.InnerException.Message);
}
return result;
}
is that something that i missing?
*note :
- my vps was connect to internet,
- i use google cloud before, and the code works well.
The following is my code to send a mail over a http proxy. I have set my proxy to the same as mentioned. Now when I try to run the program, after some time it says *System.Net.WebException: The remote name could not be resolved. May I know where I am going wrong?
protected void mailto(string message)
{
WebRequest.DefaultWebProxy = new WebProxy("10.1.1.4", 8080);
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail#gmail.com");
mail.To.Add("to_address");
mail.Subject = "For Data Using";
mail.Body = message;
SmtpServer.Port = 465;// Tried even with 587, but no luck
SmtpServer.Credentials = new System.Net.NetworkCredential("mymail#gmail.com", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Debug.WriteLine(Environment.NewLine + "message sent");
}
catch (Exception ex)
{
Debug.WriteLine("Message not sent" + ex.ToString());
}
}
I have faced same issue with sending a mail from c# using Gmail SMTP, the problem was the Port.
Try to use the port : 587
SmtpServer.Port = 587;
Also don't forget to Enable POP & IMAP Access to your Gmail account, go to settings , choose 'Forwarding and POP/IMAP' from the tabs and enable the POP & IMAP
I have been trying for 2 days to get my ASP.NET webforms application to send an e-mail.
I have tried this using both outlook and gmail. I got the smtp information for both from this tutorial:
When I try using port 587 in the example below I get an error saying:
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
When I try using port 465 in the example below:
My application just hangs forever and my page never gets back my email function which is in the PageLoad.
A couple of things to note just incase one of these is messing me up:
I am using a standard VS2013 dev environment (running my web app in debug mode using F5)
I am using the same e-mail address for from, to, and gmail login credentials)
My ISP does not block these ports. (comcast cable)
I even went to the google DisplayUnlockCaptcha page to Allow access to my account
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
using (MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress))
{
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
}
This code should work fine for you
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
You'll need to set the delivery mode otherwise gmail will return an error
EDIT:
Throwing an 'using' around 'MailMessage' might also be a smart thing to do
It turns out that it is because of a GMail security setting.
https://www.google.com/settings/security/lesssecureapps
You have to enable access for less secure apps.
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);
}
}``
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/
This code works fine on my local machine ut when I deploy it. it gives Failure sending mail error.. Please Help...
MailAddress addrsTo = new MailAddress(toEmail);
MailAddress addrsFrom = new MailAddress("XXX#XXX.com", "XXX Title");
MailMessage mailmsg = new MailMessage(addrsFrom, addrsTo);
mailmsg.Subject = mailSbjct;
mailmsg.Body = "XXX Body";
SmtpClient smtp = new SmtpClient("mail.XXX.com");
smtp.EnableSsl = false;
smtp.Port = 26;
smtp.Credentials = new NetworkCredential("XXX#XXX.com", "XXXXXXX");
try {
smtp.Send(mailmsg);
} catch (Exception exc) {
throw new XXXException(1234, "---" + exc.Message);
}
you can try this, if you are using gmail :
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
As you have mentioned in your question works fine on my local machine.
It is suggesting that the problem is of credential which you are providing to send the mail.
Edit 1
If you are using you own domain credential then it is not going to work on the server.
The user IIS should have enough authority to send mail.
IIS User More detail
http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Here is a SO link
How to send email through IIS7?