Sending Sparkpost smtp Email using c# - email

I have looked at numerous answers in stack overflow and other places and I still cannot find a good example on how to:
in C#, Successfully send email via sparkpost smtp after STARTTLS was introduced. Documentation in Sparkpost (https://www.sparkpost.com/docs/getting-started/getting-started-sparkpost/, search for starttls) refers to the following settings:
SMTP host: smtp.sparkpostmail.com
Port: 587 or 2525
Encryption: STARTTLS
Username: SMTP_Injection
Password:
In my current applications, in web.config, I configure sparkpost smtp like so:
<network host="smtp.sparkpostmail.com" port="587" userName="SMTP_Injection" password="..." enableSsl="true" />
…
This worked fine until the start of july. And then TLS 1.0 was no longer supported. So I would like to get our email working again
But how does STARTTLS come into play ?

I added the ServicePointManager.SecurityProtocol line to my smtp C# code and it all started working again. Hope this helps.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.sparkpostmail.com";
smtp.Port = 587; // 2525
smtp.EnableSsl = true;
//NOTE THE LINE BELOW
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
smtp.Credentials = new NetworkCredential("SMTP_Injection", "YOURKEY");

Related

Sending email from specific server gives socket forbidden error

I get this error
An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xxx.xxx:587 when sending email from a godaddy server.
I have seen many questions like this but what's unique here is that, this code works on my local computer. It also works on my other Go daddy hosting Server.
This original server has TLS 1.0, because i needed TLS 1.2, I purchased a deluxe hosting plan and moved my code to this new server, then i start getting this error. I've searched everywhere and used every combination of port 587, 465, 25 along with ssl = false or true.
Any ideas please?
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
MailMessage mail = new MailMessage();
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("********#gmail.com", "********");
smtpClient.EnableSsl = true;
string fromEmail = "********#gmail.com";
mail.From = new MailAddress(fromEmail, "System");
mail.To.Add(new MailAddress(toEmail));
mail.Body = body.ToString();
mail.Subject = subject;
smtpClient.Send(mail);
}
Sounds like a firewall or AV or other port blocking software preventing outbound connections to port 587. Check your server config and look in the windows event log as there might be an entry in there indicating who did the blocking.
Try to use port 2525 for 587, 465, 25. Some cloud providers disable all outbound traffic from 587, 465, 25 ports.
It seems like Godaddy is blocking emails from its servers when you use an outside smtp like smtp.gmail.com. At least that seems like the case with this Plesk Hosting Account. The other Economy Hosting works well with Gmail smtp.
Also, the emails will only send from Godaddy server, running the code locally on Visual studio gave an error.
I changed my code to this:
using (SmtpClient smtpClient = new SmtpClient("relay-hosting.secureserver.net", 25))
{
MailMessage mail = new MailMessage();
smtpClient.Credentials = new NetworkCredential("yourdomain#yourdomain.com", "****");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
string fromEmail = "yourdomain#yourdomain.com";
mail.From = new MailAddress(fromEmail, "Name");
mail.To.Add(new MailAddress(toEmail));
mail.Body = body.ToString();
mail.Subject = subject;
smtpClient.Send(mail);
}

PHPMailer not able to send send email with ec2

I'm using PHPmailer to send account verification mail, I'm using AWS ec2 instance, however, that mailer is working fine in localhost but when I upload that to server emails are not going,
at first, i used SendGrid credentials to send emails, failed, then tried Gmail SMTP, failed, and somewhere I read that ec2 can't send emails, then I created SES also, still can't able to send.
searched on the web abt that but no answers are fixing my problem,
in localhost, in can send emails with the same code and with SendGrid of Gmail credentials, why I can't send with the server?
my PHP mailer code is:
$sub = "Thankyou For registration! Confirm Your mail to Login";
$mailBody = "<h1>You are successfully registered<br />Visit site to login</h1>";
require 'mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "tls://email-smtp.us-east-1.amazonaws.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "smtp_username"; // SMTP username
$mail->Password = "smtp_password"; // SMTP password
// $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom("my_mail_id#gmail.com", "SMTP_REPLAY_NAME");
$mail->addReplyTo("my_mail_id#gmail.com", "SMTP_REPLAY_NAME");
$mail->addAddress("recipient_mail_id#gmail.com"); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sub;
$mail->Body = $mailBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
} else {
echo 'Message has been sent';
}
it shows Message has been sent but I cant receive emails, checked in spam folder also, no clue of mail!
even I have openSSL certificate also! opened SMTP port for both inbound and outbound in security group of ec2, everything working fine but PHPMailer!
Get your protocols straight. In the Host you're specifying tls, but telling it to connect to Port = 465, which will not work with TLS. Either change your Port to 587 (preferred) or change your encryption method to ssl. Enabling debug output (SMTPDebug = 2) will let you in on what's happening in the conversation with the server.
A perusal of the troubleshooting guide would probably help.

PHPMailer Update Issue (from 5.2.9 to 5.2.16)

PHPMailer works great, I've been using it for ages and it's pretty good. I have had version 5.2.9 on my server and have got the newer, 5.2.16 uploaded,
Now my sites have issues of:
"SMTP Error: SMTP connect() failed."
I have added $mail->SMTPDebug = 4; and it gives me this:
2016-11-15 17:18:10 Connection: opening to localhost:25, timeout=300, options=array (
)
2016-11-15 17:18:10 Connection: opened
...
removed excess repetative lines
...
2016-11-15 17:18:10 SERVER -> CLIENT: 250-mail.servernetwork.co.uk Hello www.domain.co.uk [::1]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
2016-11-15 17:18:10 CLIENT -> SERVER: STARTTLS
2016-11-15 17:18:10 SMTP -> get_lines(): $data is ""
2016-11-15 17:18:10 SMTP -> get_lines(): $str is "220 TLS go ahead
"
2016-11-15 17:18:10 SERVER -> CLIENT: 220 TLS go ahead
2016-11-15 17:18:10 SMTP Error: Could not connect to SMTP host.
2016-11-15 17:18:10 CLIENT -> SERVER: QUIT
2016-11-15 17:18:10 SMTP -> get_lines(): $data is ""
Nothing else has changed on the server. Reverting to PHPMailer Version 5.2.9 removes this error (and emails are sent ok).
I have also tried to update from 5.2.9 to 5.2.14 and 5.2.13 and the same error occurs. I have made no changes to the DNS and there are no DKIM or other identifiers set in the PHPmailer classes to allow version 5.2.9 but not 5.2.13 or 5.2.16 ones.
I'm using PHP Version 5.6.2.
Any clues as to why this is so?
The sending code:
(I realise the code is not the best but it's an older site I picked at random to test out PHPMailer 5.2.16)
$mail = new PHPMailer();
//$mail->SMTPDebug=4;
$mail->Host = "localhost";
$mail->WordWrap = 78;
$mail->isSMTP();
$mail->From = $fromMail;
$mail->FromName = "Website: ".$name;
$mail->AddAddress($toEmail);
$mail->Subject = $member['bizname'] . " Enquiry";
$mail->Body = $message;
if (!$mail->Send()) {
...
}
The proper way to initialize the settings is, note that you should set the $mail->SMTPAuth = true:
<?php
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com'; // Specify main SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
//Only if you have SSL/TLS on your server
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
...
After spending a day or so asking various related questions, I found the answer lies with an update applied in PHPMailer 5.2.10.
I found that while my server is TLS certified, there seems to be a problem with using a localhost Host; possibly that PHPMailer may be trying to check the certificate applies to localhost domain, which of course it doesn't.
I found a solution to sending mail via Localhost now is therefore to disable auto-TLS with the following:
$mail->SMTPAutoTLS = false;
Using this all emails now send correctly.
NOTE
As Bodi0 stated using SMTPAuth is best practise, however I didn't realise that it is unnessecary for localhost mail servers as the authenticaton is implicit.
Comments on this answer to a related question about ->SMTPSecure settings gives more details.

Send email from gmail using Telnet

I am working on windows and I have enabled telnet client
In cmd prompt:
$telnet smtp.gmail.com 587
220 mx.google.com ESMTP dk3sm50678627pbc.32 - gsmtp
$Helo
250 mx.google.com at your service
$ mail from: <myuser#gmail.com>
530 5.7.0 Must issue a STARTTLS command first. dk3sm50678627pbc.32 - gsmtp
$ STARTTLS
220 2.0.0 Ready to start TLS
$ mail from:
C:\Users\{myuser}>
Connection to host lost.
Don't know What is the problem ?
Can anyone help me out , how i can send emails from gmail server using telnet from command line >
smtp.gmail.com requires TLS. The basic telnet client that comes with windows does not know how to negotiate TLS with a server. You may want to use openssl instead, which is able to negotiate TLS. See http://www.madboa.com/geek/openssl/#cs-smtp for an example of how to do this.
The gmail smtp must use smtp auth before you sending your email. The smtp auth need username and password.
see this link blow if you can read in Chinese.
http://linxucn.blog.51cto.com/1360306/837365
Last I sugguest you use java to ask gmail smtp server to send email, It will be more easy , becasue you needn't encode the smtp auth to BASE64 or anything else.
GOGOGO, good luck :)
Put into a VBS file, ie sendmail.vbs.
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "cat#gmail.com"
emailObj.To = "cat#gmail.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
emailObj.AddAttachment "c:\windows\win.ini"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "cat"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Ccat1"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 then Msgbox "Done"
At Google's web site for GMail you have to turn this feature on for CDO to work.
At your Gmail page click Settings - Accounts and Import - Other Google Account Settings - [At very bottom of page] Allow less secure apps.
Also from memory you also have to click a link in an email the first time you use it (it's been a few years).

PHPMailer SMTP configuration

For the past 2 hours I've been looking online to see if any other people encountered this problem, and it seems a lot has, bot none of the answers are working for me.
SMTP -> FROM SERVER:220 mx.google.com ESMTP vq7sm928004oeb.13
SMTP -> FROM SERVER: 250-mx.google.com at your service, [50.57.114.141] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250 ENHANCEDSTATUSCODES
SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
SMTP -> FROM SERVER: 250-mx.google.com at your service, [50.57.114.141] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250 ENHANCEDSTATUSCODES
SMTP -> FROM SERVER:530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 vq7sm928004oeb.13
SMTP -> ERROR: MAIL not accepted from server: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 vq7sm928004oeb.13
The following From address failed: my#email.com
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMPTAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 587;
$mail->Username = "my#email.com";
$mail->Password = "password";
I've tried almost every setting for PHPMailer, but can't figure out what's still going wrong, are there any server settings I need to take care of?
I also tried the normal php mail() function, but that's not sending mail either, although when using Drupal forms it just sends an email.
For first you must configure the correct server to send emails (see at gmail.com):
SMTP server address: smtp.gmail.com
SMTP user name: Your full Gmail address (e.g. example#gmail.com)
SMTP password: Your Gmail password
SMTP port: 465 or 587
SMTP TLS/SSL required: yes
In PHPMailer:
$mail->SMTPAuth = true; // There was a syntax error here (SMPTAuth)
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 465;
$mail->Username = "YOU#gmail.com";
$mail->Password = "YOUR_GMAIL_password";
My problem was that I use the 2-step verification. So I had to remember to go to Authorised Access for my Google Account & then assign a Application-specific passwords for the domain that I'm using to send the mail.
Just noticed I typed SMTP wrong in this line $mail->SMPTAuth = true;, correcting this solved my problem.
The suggested port+protocol-combination seems wrong to me, because it's different to the official phpMailer-wiki:
Don't mix up these modes; ssl on port 587 or tls on port 465 will not
work.
In PHPMailer:
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 587; //use port 465 when using SMPTSecure = 'ssl'
$mail->Username = "YOU#gmail.com";
$mail->Password = "YOUR_GMAIL_password";
Setting up mail in php can be quite hard. have you tried using port=25?