I am using the below code to send email using google smtp details. This code works fine on my local machine but not working from the website/server.
I am using Hostgator server and I have already contacted them with no luck.
I don't want to use Hostgator service for sending mails.
using (SmtpClient smtp = new SmtpClient())
{
message.From = new MailAddress(("googleid"), "Test Label");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.Default;
smtp.Port = 587;
smtp.Host = smtp.gmail.com;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(("googleid"), ("googlepass"));
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
Hostgator support team created SPF records, that helped fix the issue.
https://support.google.com/a/answer/33786?hl=en
Related
I am using 'System.Net.Mail.SmtpClient' to send email notifications from my ASP.Net C# MVC web project using OFFICE365 EMAIL ID as Sender. It was working correctly a few months back but stop working when Microsoft Authenticator introduced for MS Apps. Please let me know in case you face this issue and better if you tell me it fixes.
SmtpClient client = new SmtpClient("smtp.office365.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("*****", "*****");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Timeout = 100000;
MailMessage msg = new MailMessage();
msg.To.Add("******");
msg.From = new MailAddress("*******");
msg.Subject = "Test";
msg.Body = "Testing";
client.Send(msg);
Exception => 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 [BM1PR0101CA0062.INDPRD01.PROD.OUTLOOK.COM]
I have an Azure function in which I am trying to send an email:
EmailHelper.SendEmail(myEventHubMessage, notifyFrom, notifyTo, environment);
This is my SendEmail method:
MailMessage mail = new MailMessage(notifyFrom, notifyTo);
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "The actual value of host";
mail.Subject = $"[{environment}]: [{keyvaultEvent.Data.VaultName}] Key Vault Event";
mail.Body = $"An event of type {keyvaultEvent.EventType} in KeyVault {keyvaultEvent.Data.VaultName} occured at {keyvaultEvent.EventTime}";
client.Send(mail);
I tried running this locally and it is working fine. I get an email. After deploying the Azure function I get an error saying No such host is known
The values for environment, notifyfrom and notifyto are coming from app settings.
Any idea on why that is happening?
To send emails from Azure VMs or Apps, use an authenticated SMTP relay service, also known as a Smart Host, with TLS support.
SMTP relay services provide an intermediary SMTP server between the mail servers of the sender and recipient. The TCP connection is established via the secure ports 587 or 443.
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("user#recipient.com", "The Recipient"));
msg.From = new MailAddress("user#sender.com", "The Sender");
msg.Subject = "Test Email from Azure Web App using Office365";
msg.Body = "<p>Test emails on Azure from a Web App via Office365</p>";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("<Office365-username>", "<Office365-password>");#insert your credentials
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
For more details, you could refer to this article.
MailMessage message = new MailMessage();
message.Subject = "test";
message.Body = "test";
message.To.Add("test#gmail.com");
message.From = new MailAddress("bob#internalhost.com");
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "172.22.0.20";
smtp.Port = 25;
smtp.Send(message);
Any idea why I might be getting an error
The remote name could not be resolved.
Clearly no resolution is required as I have specified an IP address. I can ping the IP and even telnet on port 25 and successfully send an e-mail. But, I can't send an e-mail.
I ran a wireshark trace and it doesn't look like any traffic is being send to 172.22.0.20
I use this code and its working fine on my end
Make sure that you are using the correct IP Address and port number. I think your Port Number is not correct.
(open your email id and check mail configuration)
MailMessage mail = new MailMessage("bob#internalhost.com", "Toemai#email.com");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "this is email body";
SmtpClient client = new SmtpClient("172.22.0.20");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("bob#internalhost.com", "password");
client.Port = 25;
client.Send(mail);
I tested your code with :
static void Main(string[] args)
{
try
{
MailMessage message = new MailMessage();
message.Subject = "test";
message.Body = "test";
message.To.Add("atmane.elbouachri#gmail.com");
message.From = new MailAddress("atmane.elbouachri#softeam.fr");
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Host = "127.0.0.1";
smtp.Port = 25;
smtp.Send(message);
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
And it seems good. I used smtp4dev Tools and it truks this :
220 localhost smtp4dev ready
EHLO Aepro
250-Nice to meet you.
250-8BITMIME
250-STARTTLS
250-AUTH=CRAM-MD5 PLAIN LOGIN ANONYMOUS
250-AUTH CRAM-MD5 PLAIN LOGIN ANONYMOUS
250 SIZE
MAIL FROM:<atmane.elbouachri#softeam.fr>
250 Okey dokey
RCPT TO:<atmane.elbouachri#gmail.com>
250 Recipient accepted
RSET
250 Rset completed
RSET
250 Rset completed
MAIL FROM:<atmane.elbouachri#softeam.fr>
250 Okey dokey
RCPT TO:<atmane.elbouachri#gmail.com>
250 Recipient accepted
DATA
354 End message with period
MIME-Version: 1.0
But when i put a fake IP I have an exception (witch is normal).
I think your IP is bad or you must have a problem with a firewall
Below code is working fine at my end.....
MailMessage mailMessage = new MailMessage("abc#xyz.in", "receiver#xyz.in");
mailMessage.Priority = System.Net.Mail.MailPriority.High;
mailMessage.Body = "message";
mailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient("host/IP", 25);
NetworkCredential credentials = new NetworkCredential("abc#xyz.in", "password");
smtpClient.Credentials = credentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = credentials;
smtpClient.Send(mailMessage);
If your code is running in a service application pool - you need to set the Identity under Advanced Settings in the Process Model as "Network Service" rather than the default "ApplicationPoolIdentity". If you do that, it'll work every time. If you don't, some emails work, some do not. It is a weird permission thing.
A similar issue popped up over here: System.Net.WebException: The remote name could not be resolved:
As you can see, a simple polling mechanism wrapped around a try-catch statement can gives you an opportunity try your block of method multiple times (which fails sporadically) before you report it to the user.
The problem I see is this line:
smtp.Host = 172.22.0.20;
smtp.Host expects string. So, it should be smtp.Host = "172.22.0.20"; Besides that, please add the code below so we can see error
try
{
smtp.Send(mail);
}
catch (Exception e)
{
Debug.WriteLine("Exception Message: " + e.Message);
}
Your IP address is not correct or you might be working under a firewall. Also check you DNS settings.
Maybe the reverse name is mandatory, have you tried to register a name for this IP in the host file ? (Or in your DNS of course)
Try to add:
172.22.0.20 my-smtp.example
I have written following code in codebehind aspx page to send email.I want to use google smtp server. But some how I am not receiving the mails
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Sender e-mail address.
MailAddress From = new MailAddress(txtFrom.Text);
// Recipient e-mail address.
MailAddress To = new MailAddress(txtTo.Text);
MailMessage Msg = new MailMessage(From,To);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = new System.Net.NetworkCredential
("*******#gmail.com", "**********");
client.EnableSsl = true;
client.Port = 465;
client.Send(Msg);
client.Dispose();
}
What wrong am I doing?Please help
Here is what you need to do with the SmtpClient object:
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("xxxx#gmail.com", "xxxx");
Gmail uses OAuth 2.0 you may well have to supply some sort of API key to access their mail functions.
Accessing mail using IMAP and sending mail using SMTP is often done using existing IMAP and SMTP libraries for convenience. As long as these libraries support the Simple Authentication and Security Layer (SASL), they should be compatible with the SASL XOAUTH2 mechanism supported by Gmail.
In addition to the SASL XOAUTH2 protocol documentation, you may also want to read Using OAuth 2.0 to Access Google APIs for further information on implementing an OAuth 2.0 client.
The Libraries and Samples page provides code samples in a variety of popular languages using the SASL XOAUTH2 mechanism with either IMAP or SMTP.
(source)
Here is a generic email program.
It works on Port=25.
Remember gmail is an IMAP server.
try
{
MailMessage msg = new MailMessage ();
MailAddress fromAdd = new MailAddress("fromemail#email.com");
msg.[To].Add("toemail#email.com");
msg.Subject = "Choose Session Members";
msg.From = fromAdd;
msg .IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg .BodyEncoding = Encoding.Default;
msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
msg.Body = msg.Body + "</table></center>";
SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("yourname#yourserver.com", "password");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
}
First of all, have you checked the webmail? In the sent folder? Many years ago I had the same problem, but I realized that my firewall was blocking me.
Other thing,
"SmtpClient class does not support Implicit SSL. It does support Explicit SSL, which requires an insecure connection to the SMTP server over port 25 in order to negotiate the transport level security (TLS)."
http://blog.ramsoftsolutions.com/2015/04/sending-mail-via-smtp-over-implicit-ssl.html
Source:
How can I send emails through SSL SMTP with the .NET Framework?
Regards
What are needed to send an email from my asp.net site? this question asked because I have a site for sending email , it works correct in local machine , but when i uploaded it on a shared server it does not work and give me this error : Failure sending mail
now i have some question about this problem :
1 - Is it SSL always required to send an email by SMTP?
2 - what about the first question if i use my gmail account?
3 - is there some configuration on my host PleskPanel to send email ?
4 - after contact with my host support team they said me that my code has problem , but i know my code is correct , here is my code
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
client.Host = "smtp.datagts.net";
client.Port = 587; // setup Smtp authentication
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("info#datagts.net","*****");
client.UseDefaultCredentials = false;
client.Credentials = credentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress("info#datagts.net");
msg.To.Add(new MailAddress("foroughi.ali#gmail.com"));
msg.Subject = "Test";
msg.IsBodyHtml = false;
msg.Body = "Body is here";
client.Send(msg);
Note : for more info about my problem i asked another question about this issue that i have not get any correct answer