error sending email using ASP.NET web app - asp.net

I'm trying to send email to my site users (ASP.NET, VS2010), currently I'm using my gmail account for sending email, but I'm getting the following error:
ex = {"Failure sending mail."}
InnerException = {"The remote name could not be resolved: 'smtp#gmail.com'"}
it is my code:
MailMessage mailObj = new MailMessage("mygmailaccount#gmail.com", "myyahooaccount#yahoo.com", "test", "test2");
SmtpClient SMTPServer = new SmtpClient("smtp#gmail.com");
SMTPServer.Credentials = new System.Net.NetworkCredential("mygmailaccount", mygmailpassword);
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
string a = ex.Message;
}
what is going wrong here? should I do something in my web.config? how can I find smtp server of my own host?

smtp#gmail.com is incorrect. You probably meant smtp.gmail.com. See the following question for a complete example.
Sending email in .NET through Gmail

you did not set gmail parameters correctly:
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("myGmailAcconut#gmail.com", "password");

Related

Sending Email through SmtpClient smtp.office365.com Generating Error in ASP.Net, C# web forms

Anyone can help please.
I am using the following code for sending email from my ASP.Net, C# web forms application which generating the error. The same code is working for other application. My domain name contains a hyphen in it (-). Is that the reason? Do I need to see email configuration or server/domain level settings? Please help.
string smtpServer = "smtp.office365.com";
int port = 587;
using (MailMessage mail = new MailMessage())
{
using (SmtpClient SmtpServer = new SmtpClient(smtpServer))
{
mail.From = new MailAddress(_emailIDFrom);
mail.To.Add(_emailIDTo);
mail.Subject = _subject;
mail.IsBodyHtml = true;
mail.Body = _emailBody;
SmtpServer.Port = port;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(_emailIDFrom, _emailIDFromPassword);
SmtpServer.EnableSsl = true;
try
{
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
return false;
}
}
}
My Tls code in Global.asax Application_Start as follows
if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
{
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}
ERROR--
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant. Visit https://aka.ms/smtp_auth_disabled for more information. [DXXP273CA0023.AREP273.PROD.OUTLOOK.COM]

How to send Email using gmail account in Asp.net WEB API

I am trying to send bulk of emails through smtp server using gmail account, but i am facing issue like this: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. ry1sm18393619pab.30 - gsmtp". I am using Asp.net c# console application.
here is my code which i am using:
static void Main(string[] args)
{
EmailSend send = new EmailSend();
send.Page_Load();
}
protected void Page_Load()
{
try
{
MailMessage msg = new MailMessage(new MailAddress("abc#gmail.com"), new MailAddress("abc#gmail.com"));
msg.Subject = "Test link..";
msg.IsBodyHtml = true;
string emailid = "abc#gmail.com";
string password = "abc123";
string host = "smtp.gmail.com";
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = host;
smtpclient.EnableSsl = true;
smtpclient.Port = 587;
smtpclient.UseDefaultCredentials = false;
smtpclient.Credentials = new System.Net.NetworkCredential(emailid, password);
smtpclient.Send(msg);
Console.WriteLine("A link has been send to you for resetting password. Check your mail! ");
}
catch (Exception ex)
{
Console.WriteLine("Could not send the e-mail - error: " + ex.Message);
}
}
I don't understand why this issue is coming. I have given correct credentials like: Emailid and password for gmail, i have changed port number also 25,587. If anyone has some sloution then it would be very helpful.

Sending Mail via Mail Server Asp.net mvc4

I am trying to send mail through mail server ( available in fatcow).
Username: info#efernssolutions.com
SMTP Server: smtp.hostname.com
SMTP Port: 587
I used the follwing code to send mail ,
if (ModelState.IsValid)
{
MailMessage message = new MailMessage();
try
{
message.To.Add(new MailAddress("kishore.eferns#gmail.com"));
message.From = new MailAddress("info#efernssolutions.com");
message.Subject = "Test mail";
message.Body = "this is the mail sended through my c#.net program";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.hostname.com";
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("info#efernssolutions.com", "password");// is u are using Gmail
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = nc;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
client.Send(message);
}
catch (Exception ex)
{
;
}
}
When i use gmail server the code works fine and i received mail.
But it is not working with my email server.
I get the error,
the remote certificate is invalid according to the validation procedure
Is there anything left to do to make it work?
Please help,
Thank you
As first check I will suggest to check if your SSL certificate is correctly installed on your server

How Can i Send Mail Through Exchange Server by using SMTP

I want to Run Below code without
NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD").
BY using Only Exchange Host (Server Name) And Port
Im Getting Error For this code : Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender
protected void SendEmail(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient("ExchangeServerName",25);
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("bala#OfficeName.com", "From Me");
MailAddress toAddress = new MailAddress("bala#OfficeName.com", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.UseDefaultCredentials = true;
System.Net.NetworkCredential nc = CredentialCache.DefaultNetworkCredentials;
smtpClient.Credentials = (System.Net.ICredentialsByHost)nc.GetCredential("ExchangeServerName", 25, "Basic");
smtpClient.Send(message);
lblText.Text ="Email sent.";
}
catch (Exception ex)
{
lblText.Text = "Coudn't send the message!\n " + ex.Message;
}
}
I have done it. For more details about my code use this link.
Below Code is worked Fine with
Server : Windows Server 2003,Windows Server 2008,Windows Server 2008 R2
IIS : 6.0, 7.0
.Net Frame Wotk : 2.0,3.5,4.0
string sMessage;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
//you can provide invalid from address. but to address Should be valil
MailAddress fromAddress = new MailAddress("bala#technospine.com", "BALA");
smtpClient.Host = "Exchange Server Name";
smtpClient.Port = 25;
//smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
message.From = fromAddress;
message.To.Add(bala#technospine.com); //Recipent email
message.Subject = _subject;
message.Body = _details;
message.IsBodyHtml = true;
//smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
sMessage = "Email sent.";
}
catch (Exception ex)
{
sMessage = "Coudn't send the message!\n " + ex.Message;
}
lblMailStatus.Text = sMessage;
You are attempting to send a mail message using Exchange. In order to do that, the sender (or sending process) must have permissions on the account it is logged in under to send on behalf of the user you are specifying as the sender. This is different from going through Exchange's SMTP mail transfer agent (MTA) in order to have Exchange receive and route an email message. So you are on the right track with knowing you should do this using SMTP, but you are just trying to use the wrong API for accomplishing this. You want to take a look at CDOSYS for sending it through the SMTP MTA without having to do user authentication. Search on System.Web.Mail.MailMessage for more specific examples - there are plenty out there. If the Exchange server does not seem to accept/deliver the SMTP message delivered to it in this fashion, you might simply need to open up its configuration a bit. In that event, the Exchange server is probably configured with tight security on routing of mail received via its SMTP MTA and just needs to have the IP address of the machine(s) you are sending these messages from configured to allow for mail forwarding.
try NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD","DOMAIN")

Sending e-mail in ASP .Net

How to send e-mail on ASP .Net using outlook address??
I've tried this code but no luck:
Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))
mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)
smtpClient.Send(mailMessage) //--> got error here
But while I'm execute the code, it got this error 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
I've tried to add a line of code:
smtpClient.Credentials = New System.Net.NetworkCredential(username, password)
But still can't send the e-mail.
Anyone can help?
Try smtpClient.UseDefaultCredentials = false; before you set new Credentials
Try to set smtpClient.EnableSsl to true / false depending on your environment
I'm guessing you are using Exchange 2007 or later as backend?
Anyway, your mail server does not allow you to send mails anonymously. You'll either need to supply a username/password in your code or allow unauthenticated relaying from your webserver.
Talk to your IT guys what they prefer.
I did it using c#.This may help you.Please check.
MailMessage msg1 = new MailMessage();
msg1.From = strEFrom;
msg1.To = strETo;
msg1.Cc = strECC;
msg1.Subject = "Hi";
msg1.Priority = MailPriority.High;
msg1.BodyFormat = MailFormat.Html;
msg1.Body = strBody;
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
SmtpMail.Send(msg1);
and in web.config file
<appSettings>
<add key="MailServer" value=""/>
</appSettings>
Try these settings check for .EnableSSL property to true/false and the smtp post number on which your mail server listen for outgoing mail.
When you do not set enable the SSL settings in outlook for gmail outlook configuration then it gives same error but the reason was found the SSL settings..
well try this may it will solve your problem..
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
if it does not solve your problem then check your mail server/ client for the problem.
public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
I use this code to send mail from my gmail account.
was having the same error, moved the client.UseDefaultCredentials = false; before setting client.Credentials and everything works.

Resources