My code is below:
MailMessage mail = new MailMessage();
mail.To.Add("******");
mail.CC.Add("*****");
mail.From = new MailAddress("*******");
mail.Subject = "Salesforce Credential for ";
mail.SubjectEncoding = Encoding.UTF8;
string Body = "<html><head></head><body>Hi,<br> Project Name: "
+ " <br> Username : "
+ " <br> Password : "
+ " <br> Security Token : ";
mail.Body = Body;
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "webmail.***************.**.**";
string uid = "***********";
string pwd = "*********";
smtp.Credentials = new System.Net.NetworkCredential(uid, pwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
// without this I get: The remote certificate is invalid according to the validation procedure.
smtp.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(#"********"));
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
But I got this error:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
You are using SMTP not HTTP to send this message. SMTP typically does not use proxies but rather relays. I suspect the fundamental issue, based on your code, is that webmail.whateaver.xx.yy is the exchange web services endpoint and it is not capable nor configured to expose the SMTP port publicly. You'll need to get with whoever controls that system and work out what the proper means of access are.
If they can't expose SMTP -- which is higly likely, I would laugh at your face if you came and asked me to do that -- you probably will need to find an alternate outbound SMTP option or use Exchange Web Services to send the mail. See nuget for EWS client libraries.
Finally, while I've got the floor, please do the world a favor and do not email passwords around.
Related
We are trying to send mail through SMTP setup as below
SmtpClient smtpClient = new SmtpClient(SMTPServer, SMTPPort);
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(SMTPUserName, SMTPassword);
MailAddress mailAddress = null;
if (!string.IsNullOrEmpty(sFromEmail))
mailAddress = new MailAddress(sFromEmail);
else
mailAddress = new MailAddress(SMTPEmailID);
MailMessage mailMessage = new MailMessage();
mailMessage.From = mailAddress;
mailMessage.Subject = sEmailSubject;
foreach (string toAddress in sToEmail.Split(';'))
{
if (toAddress.Trim() != string.Empty)
mailMessage.To.Add(toAddress);
}
mailMessage.IsBodyHtml = true;
mailMessage.Body = sEmailBody;
smtpClient.Send(mailMessage);
logText.Add(Environment.NewLine);
logText.Add("Mail sent successfully at " + DateTime.Now.ToString());
logCreation.createLogFileFromList(logText);
logText.Clear();
sErrorMessage = "Mail sent successfully";
but this end up with error
Mailbox unavailable. The server response was: 5.7.54 SMTP; Unable to
relay recipient in non-accepted domain
any setup I am missing here..
First
Email gateway is a SMTP server in charge of filter virus or spam. Email Gateway user maybe included spam list check this
Second
If you change your mail server ip address,your domain system may not have generalization dns in your exchange system.
Third
If you change your mail server ip address,your domain system may not have generalization your exchange system. You must be force generelization dns.
At this point can you check your project in running server
open powershell and edit your email info then run this query
send-mailmessage -to '<test#test.com>' -From '<admin#test.com>' -subject "TEST" -body "hi" -smtpServer mail.yourlocalsmtpdns.com
if you take this error change mail.yourlocalsmtpdns.com to your smtp ip address
if you not take error. You can think that your problem cause smtp domain address.
Fast solution,
you can connect to smtp with smtp ip address to your project.
I've read a lot of blogs & tutorials on this subject, but it still doesn't work. I'm attempting to send email using Office365's SMTP server.
This is my code
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("EmailAddress", "Password", "domain.com");
smtpClient.Port = 587; // 25 587
smtpClient.Host = "smtp.office365.com";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Timeout = 600000;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("fromemailaddress", "Enquiry");
mailMessage.To.Add(new MailAddress("toemailaddress", "Test"));
mailMessage.Subject = "Inquiry";
mailMessage.Body = "\r\nName:" + user.Name + "\r\nMessage:" + user.Message
+ "\r\nContact No.:" + user.MobileNo + "\r\nEmail Address:" + user.Email;
mailMessage.IsBodyHtml = true;
smtpClient.Send(mailMessage);
I got an exception:
Unable to connect to the remote server
Failure sending mail.
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 51.92.122.216:587
I am a newbie in this. I try to ping the domain it's listening and I successfully login in the browser with same credentials.
Anything I am missing to set up after login via Office 365?
Thank you
You code looks good, and as long as you have followed these guides (correct host and ports). You should be fine.
But be aware, smtp is disabled by default, and you have to enable it for your account.
Also if you have configured MFA (so a token or a text) you’re out off luck, since it won’t accept your password. If that is the case you need to create a special app password as explained here.
I published the site I completed using ASP.NET Web Forms but I'm having trouble sending mail. My web site does not send mail. There is no problem when I run in local.
My Fonksiyon.cs:
public static bool MailGonder(string gonderenaciklama, string kimemail, string kimeadi, string mailkonu, string mailicerik, string kimdenmail = "", bool IletisimFormuMu = false)
{
MailAddress From = new MailAddress(IletisimFormuMu ? kimdenmail : "My e-mail address is here", gonderenaciklama); // Gönderen kısmında görünen e-posta adresi.
MailAddress To = new MailAddress(kimemail, kimeadi); // Mailin gönderileceği adres.
MailMessage EMail = new MailMessage(From, To);
EMail.Subject = mailkonu;
EMail.Body = mailicerik;
EMail.IsBodyHtml = true;
EMail.BodyEncoding = Encoding.Unicode;
SmtpClient MailClient = new SmtpClient();
MailClient.Port = 587;
MailClient.Host = "smtp.gmail.com";
MailClient.EnableSsl = true; // Gmail üzerinden gönderme yapılacaksa veya sunucu kimlik doğrulaması gerektiriyorsa buraya true değerini vereceğiz.
MailClient.UseDefaultCredentials = true;
MailClient.Credentials = new System.Net.NetworkCredential("My e-mail address is here", "My password is here"); // Maili göndereceğimiz hesap bilgileri buraya giriyoruz. Mailimiz bu hesap üzerinden gönderilecek.
MailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
MailClient.Send(EMail);
return true;
}
catch
{
return false;
}
}
My register.aspx button click:
string guid = Guid.NewGuid().ToString();
Fonksiyon.MailGonder("Ay Tasarım E-Posta Doğrulaması", TxtEPosta.Text, TxtAd.Text + " " + TxtSoyad.Text, "E-Posta Doğrulaması", "Lütfen aşağıdaki aktivasyon kodunu sitemizdeki ilgili alana yazarak üyeliğinizi aktif ediniz!<br />Aktivasyon Kodu: " + guid + "");
Nothing to do with your code, this is security feature of your gmail account.
these are the reasons that you can check
Google's security system has blocked the IP of your server
Google security system is actually pretty cool, if somebody gets his hands on your Gmail's password, well he won't be able to do much, unless he is using your IP address. Why? Because when Google spots an unusual IP address trying to connect to your account it will deny it access and will send you an email and eventually a text message on your mobile phone.
When you send a test email from MailPoet's Settings and you get the following message : " SMTP Error: Could not authenticate. | SMTP Error: Could not connect to SMTP host." then you might be entering this case scenario
The email you will receive to notify you of that unusual access will be as follow :
Allow new IP's in Google account
In your case when you setup your site to send with your Gmail account, you want to allow a new IP to use your Gmail's credentials. In order to allow a new unrecognized app simply go to https://security.google.com/settings/security/activity, find the line that concerns you and allow access.
Hope this helps to resolve your problem....
I have tried most of the tweaks advised but none work.
I have tried :
1.Enabling 2-step verification on gmail
2.Enabled less secured app on gmail
3.Changed password to strong
Earlier I made an account for in gmail and by mistake entered a wrong phone number.But that email workjed fine on my C# application, I was able to send email. But, as I am India and client in US, so had put location as US. So when logged into gmail it logged me out saying some suspicious activity So I had to create a new account.
But since then it gave me an error:
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 :
SmtpClient client = new SmtpClient(txtsmtp.Text);
c ServicePointManager.ServerCertificateValidationCallback =delegate(object s, X509Certificate certificate,X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
client.UseDefaultCredentials = false;
client.Port = Convert.ToInt16(txtport.Text);
client.Credentials = new System.Net.NetworkCredential(txtuserid.Text, txtpassword.Text);
client.EnableSsl = true;
client.Timeout = 200000;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
MailMessage mail = new MailMessage();
mail.To.Add(txtsendto.Text);
mail.From = new MailAddress(cmbuseremailaddress.Text);
mail.Subject = txtsubject.Text;
mail.Body = txtmessage.Text;
mail.Bcc.Add(textBox1.Text);
mail.Priority = MailPriority.High;
try
{
client.Send(mail);
MessageBox.Show("Mail Sent Successfully.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Here port no I am using is 587
So please let me know where and what I am missing.
Thanks
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);