This is my code to send email after registration is successful. It works fine on my localhost IIS server. But after deploying web-site on server email is not sent to user. There is no exception or error message shown.
MailMessage mm = new MailMessage("xyz#gmail.com", TextBoxEmail.Text.Trim());
mm.Subject = "Password Recovery";
mm.Body = string.Format("Hi ,<br /><br />Your password is .<br /><br />Thank You.");
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "xyz#gmail.com";
NetworkCred.Password = "xyz";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
message = "Registration successful. Activation email has been sent.";
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
same code work on password recovery page. but here it is not working.
I did some changes in code and also create a new page for registration now it is showing a following error
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
first i thought that it is error of mail sending code but it work fine on iis local server and also on other page. so i think may be it is a problem of button click event. just able to reach here. please some help me as i know i get my answer here.
my new code:
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("xyz#gmail.com");
mail.Subject = "mailSubject";
mail.Body = "mailBody";
mail.IsBodyHtml = true;
mail.To.Add("xyz#gmail.com");
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))//2nd parameter is PORT No.
{
smtp.Credentials = new System.Net.NetworkCredential("xyz#gmail.com", "xyz");
smtp.EnableSsl = true;//set this as your Host Name properties, for gmail,its true
smtp.Send(mail);//actual sending operation here
}
}
Be sure to use System.Net.Mail, not the deprecated System.Web.Mail.
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Also, You have to create a specific Google password for that app via:
Google> Account> Security> Apps> Manage apps> Add an app
Select a name My Awesome App and Google will generate a custom password.
Another advice is Allow Less Secure Applications in your google account: link
And finally, take in mind that Google don't allow you to send more than 250 messages per hour, no more than 1000 per day (not sure of the last number).
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
}
}
}
The code I currently have is:
public void SendEmail(string to, string cc, string bcc, string subject, string body, string attachmentPath = "", System.Net.Mail.MailPriority emailPriority = MailPriority.Normal, BodyType bodyType = BodyType.HTML)
{
try
{
var client = new System.Net.Mail.SmtpClient();
{
client.Host = "smtp-mail.outlook.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("[my company email]", "[my password]");
client.Timeout = 600000;
}
MailMessage mail = new MailMessage("[insert my email here]", to);
mail.Subject = subject;
mail.Body = body;
client.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The email address I'm trying to send to is hosted on Office 365's Outlook. We might have to change the specific address later, but they'd likely be configured the same.
However, whenever I try to run the client.Send(mail); command, I receive the same error. The full text of the error is:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
I've tried a few different things, like switching the port between 25 and 587, changing the host to Office365's, or toggling UseDefaultCredentials and EnableSssl to true or false. But I always see the same error. Is there something else I'm missing?
I found an example code block elsewhere on this site and replacing everything I had with it made the difference.
The function name and parameters were the same, but here's what I replaced the body of it with.
var _mailServer = new SmtpClient();
_mailServer.UseDefaultCredentials = false;
_mailServer.Credentials = new NetworkCredential("my email", "my password");
_mailServer.Host = "smtp.office365.com";
_mailServer.TargetName = "STARTTLS/smtp.office365.com";
_mailServer.Port = 587;
_mailServer.EnableSsl = true;
var eml = new MailMessage();
eml.Sender = new MailAddress("my email");
eml.From = eml.Sender;
eml.To.Add(new MailAddress(to));
eml.Subject = subject;
eml.IsBodyHtml = (bodyType == BodyType.HTML);
eml.Body = body;
_mailServer.Send(eml);
I don't know for certain but I think that replacing the Host value with the smtp Office 365 link rather than an outlook one, as well as remembering to add a Target Name which I did not have before, both did the trick and solved the authorization issue (I had previously confirmed it wasn't a credentials issue with our tech support).
Good day, I'm a beginner from using ASP.net and SMTP Mailer
Heres my Question, I always encounter this Error when i send email from my local
and searched and tried the solutions around the net but not so lucky,
I hope someone point out what codes do i need and where i encounter this errror
Message = "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"
Heres my Code:
protected void btnSendEmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Value, txtName.Value);
smtpClient.Credentials = new System.Net.NetworkCredential("myUser#gmail", "password");
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
//Default port will be 25
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("myEmail#gmail.com");
message.Subject = txtSubject.Value;
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1#yoursite.com")
message.CC.Add("myEmail#gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("myEmail#gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtaMessage.Value;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.HeadersEncoding = System.Text.Encoding.UTF8;
// Send SMTP mail
smtpClient.Send(message);
lblSuccess.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblSuccess.Text = "Send Email Failed.";
}
}
i tried to make a simple codes for sending email try this
MailMessage mm = new MailMessage();
mm.From = new MailAddress("fromEmail");
mm.To.Add("toEmail");
mm.CC.Add("ccEmail");
mm.Subject = "StringSubject";
mm.Body = "BodySubject";
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "UsernameString";
NetworkCred.Password = "PasswordString";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
try
{
smtp.Send(mm);
}
catch (Exception)
{
}
Try this reference ASP Simple SMTP for C# and VB it helps me a lot for may smtp problem
Please have a look on google support team, what they are saying regarding sending mail from application.
https://support.google.com/mail/answer/78775?hl=en
Also following link can help you.
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Goto Account of Gmail , then select
Connected apps & sites
Allow less secure apps: ON(if this is off you cannot send mails through apps,or your websites )
I am trying the send email code in asp.net mvc but I keep getting the error {"Failure sending mail."} I have referred to many of the questions asked here and tried out the suggested sollutions but still got the same error. Where am I going wrong?
Here is my code:
private static void SendEmailWithErrors(string result)
{
SmtpClient smtpclient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("email address");
smtpclient.Host = "smtp.gmail.com";
smtpclient.Port = 587;
smtpclient.UseDefaultCredentials = true;
smtpclient.EnableSsl = true;
smtpclient.Credentials = new System.Net.NetworkCredential("email address", "password");
message.From = fromAddress;
message.To.Add("email address");
message.Subject = "Exception raised";
message.IsBodyHtml = false;
message.Body = result;
smtpclient.Send(message);
}
catch(Exception ex)
{
}
}
Check in to your Email ID from which you are sending the message from. There is a chance there is a warning mail sent to you that someone is trying to illegally access your mail. Just make sure to lower your security for your email to allow 3rd party apps to access it. In case of Gmail, this is done in privacy settings.
It should work fine once the security of the from email address is lowered.
private static void SendEmail(string result)
{
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = "Mail Subject",
Body = result
})
{
smtp.Send(message);
}
}
Thank you all for posting your answers, I am using the local machine and the IP address is already configured in the server machine, so that was causing the conflict. The code works fine when I run the application from the server machine.
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/