Send email from my asp.net site uploaded on a shared server - c#

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

Related

email sender c# code not working in asp.net mvc web app

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]

smtp.gmail.com - not working from the website - Hostgator server

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

No Such Host is known Azure functions

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.

Send email using asp.net and google smtp server

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

Hmailserver C# SMTP

I'm just trying to get my hmailserver to send mail from my C# program. The part that's killing me is the SSL part.
I originally got this error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.
So I added: smtp.EnableSsl = true; and now I get Server does not support secure connections.
Here is my code, this is driving me nuts. Do I have to create my own SSL or is there a way to disable SSL on hmailserver side?
MailMessage mail = new MailMessage("jlnt#ademo.net", "com", "NEW Item", emailBody);
SmtpClient smtp = new SmtpClient("1.1.1.250");
smtp.Port = 25;
NetworkCredential login = new NetworkCredential("ja#test.net", "dg");
smtp.Credentials = login;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
Ahh okay what you have to do is in HMailServer go to advanced- ip ranges. Create a new IP range for example if you 192.168.1.2, you have to make the range 192.168.1.1-192.168.1.3, then at bottom uncheck all the required smtp authentication boxes.
Annoying...
To enable secure connection to send email throught your email provider, you have to change the port number.
MailMessage mail = new MailMessage("jlnt#ademo.net", "com", "NEW Item", emailBody);
SmtpClient smtp = new SmtpClient("1.1.1.250");
//smtp.Port =25;
smtp.Port =587;
NetworkCredential login = new NetworkCredential("ja#test.net", "dg");
smtp.Credentials = login;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
i was having this issue, what i did was used localhost ip and EnableSsl to false
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "127.0.0.1";
smtpClient.Credentials = new NetworkCredential("test#123test.com", "pass123");
smtpClient.EnableSsl = false;
// then your other statements like: from, to, body, to send mail
this guide will help you setup custom NetworkCredentials in HMailServer as used above, hope helps someone.
I have stumbled on this question when trying to configure hMailServer to work to e-mail sending from C#. I have tried the following:
C# SmtpClient - does not work with implicit SSL - see this question and answers
AegisImplicitMail from here - could not make it work with UTF-8 strings (I have diacritics in my strings)
MailKit from here - very powerful and mature, no problems using it
I aimed for the following:
decent security
being able to send e-mails to mainstream e-mail providers (e.g. Google, Yahoo) and reach Inbox
being able to receive e-mails from mainstream e-mail providers
C# code
public void MailKitSend(string senderEmail, string senderName, string subject, string bodyText, string receivers, string receiversCc)
{
// no receivers, no e-mail is sent
if (string.IsNullOrEmpty(receivers))
return;
var msg = new MimeMessage();
msg.From.Add(new MailboxAddress(Encoding.UTF8, senderName, senderEmail));
msg.Subject = subject;
var bb = new BodyBuilder {HtmlBody = bodyText};
msg.Body = bb.ToMessageBody();
IList<string> receiversEmails = receivers.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string receiver in receiversEmails)
msg.To.Add(new MailboxAddress(Encoding.UTF8, "", receiver));
if (!string.IsNullOrEmpty(receiversCc))
{
IList<string> receiversEmailsCc = receiversCc.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string receiverCc in receiversEmailsCc)
msg.Cc.Add(new MailboxAddress(Encoding.UTF8, "", receiverCc));
}
try
{
var sc = new MailKit.Net.Smtp.SmtpClient();
if (!string.IsNullOrWhiteSpace(SmtpUser) && !string.IsNullOrWhiteSpace(SmtpPassword))
{
sc.Connect(SmtpServer, 465);
sc.Authenticate(SmtpUser, SmtpPassword);
}
sc.Send(msg);
sc.Disconnect(true);
}
catch (Exception exc)
{
string err = $"Error sending e-mail from {senderEmail} ({senderName}) to {receivers}: {exc}";
throw new ApplicationException(err);
}
}
hMailServer configuration
1) Opened ports - 25, 143, 465, 995 are opened to ensure that you can send and receive e-mail
2) TCP/IP ports configuration
SMTP / 0.0.0.0 / port 25 / no security (allow receiving start process)
SMTP / 0.0.0.0 / port 465 / SSL/TLS security (must define a SSL certificate)
POP3 / 0.0.0.0 / port 995 / SSL/TLS security (use the same SSL certificate)
3) pre C# testing
Run Diagnostics from hMailServer Administrator
Use an e-mail client that allows manual configuration of various settings such as ports for each protocol, security. I have used Thunderbird. Include sending of e-mails to external providers and receiving e-mails from them (I have tried with Gmail).
I made no changes in IP ranges and left the implicit ones (My computer and the Internet).
Although it's 7 years passed since the accepted answer was posted - I also upvoted it in the beginning - I want to emphasize that the suggested solution disables the whole authentication process which is unnecessary. The problem is the line with :
smtp.UseDefaultCredentials = false;
Just remove that line and it should work.
I post here the working solution for me (note that I'm not using SSL):
MailMessage mail = new MailMessage("a1#test.com", "foooo#gmail.com");
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("a1#test.com", "test");
client.Port = 25;
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "...IPv4 Address from ipconfig...";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

Categories

Resources