I am using System.Net.Mail.SmtpClient to send mail in my Xamarin Form app. It's set to using my gmail address, and working great.
I would like to get the error from the smtp server (if there is one) to inform the user.
So, I am using the event _SendCompleted
Here my code
(sending email)
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(outils.emailEmetteur);
mail.To.Add("toto#outlook.frfr");//Tiers.contactEmail);
mail.Subject = outils.emailObjetDefaut;
mail.Body = outils.emailMessageDefaut;
bool fileExist = File.Exists(filePath);
if (fileExist)
{
Attachment pJ = new Attachment(filePath);
mail.Attachments.Add(pJ);
smtpServer.Port = 587;
smtpServer.Host = "smtp.gmail.com";
smtpServer.EnableSsl = true;
smtpServer.UseDefaultCredentials = false;
smtpServer.Credentials = new NetworkCredential(outils.emailEmetteur, outils.emailEmetteurMDP);
try
{
smtpServer.SendAsync(mail, null);
smtpServer.SendCompleted += smtpServer_SendCompleted;
return true;
}
catch (Exception ex)
{
}
}
(send completed event)
private static void smtpServer_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
string token = (string)e.UserState;
string info = "";
if (e.Cancelled)
{
info = "Send canceled.";
}
if (e.Error != null)
{
info = e.Error.ToString();
}
else
{
info = "Message sent.";
}
}
I am trying to send an email to an incorrect address (toto#outlook.frfr)
My event is correctly triggered, but e.Cancelled and e.Error are NULL, and, in my Gmail inbox, I receive an error from smtp server telling me that the email address was incorrect, and that what I want to get in my Xamarin app.
Do you have any idea ? Thanks :)
Your client relays it to Gmail's outgoing mail server; the transaction to submit the message to SMTP is successful. The error only happens later, when Gmail tries to connect to the recipient's mail server, and can't resolve it; at that point, your client is no longer necessarily connected, and so the mail server generates a bounce message and delivers it to the sender's inbox.
In the general case, you either have to write your own mail server (but no, don't go there) or examine the inbox for bounce messages.
I tried to use example (with my login and pass) from http://dlang.org/phobos/std_net_curl.html#.SMTP
But every time I am getting error: std.net.curl.CurlException#std\net\curl.d(3691): Failed sending data to the peer on handle 7F6D08.
What's wrong? I tried to specify port (465) but it's not helped.
import std.net.curl;
// Send an email with SMTPS
auto smtp = SMTP("smtps://smtp.gmail.com");
smtp.setAuthentication("from.addr#gmail.com", "password");
smtp.mailTo = ["<to.addr#gmail.com>"];
smtp.mailFrom = "<from.addr#gmail.com>";
smtp.message = "Example Message";
smtp.perform();
This is the issue what i am facing
Localhost
Test mail with SMTP settings work
New user creation mail using the above smtp settings work
Server
Test mail with SMTP settings work
New user creation mail using the above smtp settings doesn't work
I echoed the mail smtp settings in both the cases and they are exactly same. The error i am getting is
SMTP -> ERROR: Failed to connect to server: php_network_getaddresses:
getaddrinfo failed: Name or service not known (0)SMTP Connect()
failed.
Any suggestions would be helpful.
I further debug it. The behavior turns out to be wierd
if (isset($_POST['User']))
{
if (UserUtil::validateAndSaveUserData($model, $_POST))
{
$mailer = new UiMailer();
$mailer->setFrom('fromAddress', 'fromName');
$mailer->setTo('toaddress');
$mailer->setSubject('Test subject');
$mailer->setBody('Test Body');
$mailer->Mailer = 'smtp';
$mailer->Username = 'username';
$mailer->Password = 'password';
$mailer->Host = 'host';
$mailer->Port = 25;
$mailer->SMTPAuth = true;
$status = $mailer->send() ? true : false;
if($status == true)
{
print "Sucess";
}
else
{
print $mailer->ErrorInfo . "</br>";
print "Failuere";
}
exit;
}
}
If i comment the call if (UserUtil::validateAndSaveUserData($model, $_POST)), it works fine. In the function i am validating and saving models using Yii framework. I further debug the function. I have the following relation in the system
User has one person
User has one address
So in the above call, if i comment the address part which $model->address->attributes or $model->address->validate or $model->address->save(), it works fine. The save functionality for address works fine. There are no issues related to it.
I have C++ code that utilizes CDO to send email based upon triggering events. I've included portions of the code below. The problem is that when app run on a server with embedded dashes such as SERVER-ABC-123 some mail servers at customer sites will reject the request with a 550 error for relayed denied. The HELO packet contains the server name with the dashes stripped which I think is the root cause of the problem.
Is there anyway to force CDO to include the FQDN?
Here is SMTP trace info
Client => Server: HELO SMCABUTELC03\x0d\x0a
Server => Client: 250 int2.unimax.pvt Hello sm-cabu-telc03.swna.wdpr.unimax.com [192.168.24.25], pleased to meet you\x0d\x0a
Client => Server: MAIL FROM: \x0d\x0a
Server => Client: 550 5.7.1 ... Relaying denied. MAIL FROM: corp.2n.admin#sm-cabu-telc03.swna.wdpr.unimax.com does not end with SMCABUTELC03\x0d\x0a
CDO::IMessage* cdoMessage = NULL;
CDO::IConfiguration* cdoConfiguration = NULL;
cdoConfiguration->get_Fields(&cdoFields);
cdoFields->get_Item( _variant_t( cdoSendUsingMethod), &cdoField1);
cdoField1->put_Value( _variant_t( (long) CDO::cdoSendUsingPort));
cdoField1->Release();
cdoField1 = NULL;
cdoFields->get_Item( _variant_t( cdoSMTPServer), &cdoField2);
cdoField2->put_Value( _variant_t( emailHostName.gets()));
cdoField2->Release();
cdoField2 = NULL;
cdoFields->get_Item( _variant_t( cdoSMTPServerPort), &cdoField3);
cdoField3->put_Value( _variant_t( emailHostPort));
cdoField3->Release();
cdoField3 = NULL;
cdoFields->get_Item( _variant_t( cdoSMTPAuthenticate), &cdoField4);
if ( emailUserName.size() == 0 )
{
cdoField4->put_Value( _variant_t( (long) CDO::cdoAnonymous));
}
else
{
cdoField4->put_Value( _variant_t( (long) CDO::cdoBasic));
}
cdoField4->Release();
cdoField4 = NULL;
I want to send email with my gmail account, I gave it a try, but no luck, so is anyone can give me a sample? Any suggestions would be appreciated. Thank you
I used lualogging api, the code is
require"logging.email"
logger = logging.email {
rcpt = "aaa#sina.com",
from = "bbb#gmail.com",
user = "bbb#gmail.com",
password = *****,
server = "smtp.gmail.com",
port = 587,
headers = {
rcpt = "aaa#sina.com",
from = "bbb#gmail.com",
subject = "[%level] logging.email test",
},
}
logger:error("error!")
You should look at LuaSocket, especially its SMTP module which can be used to send mail using your GMail account. You also need a SSL library, I use LuaSec which was designed to be used together with LuaSocket. This is the code I successfully used to send emails using my GMail account:
-- Michal Kottman, 2011, public domain
local socket = require 'socket'
local smtp = require 'socket.smtp'
local ssl = require 'ssl'
local https = require 'ssl.https'
local ltn12 = require 'ltn12'
function sslCreate()
local sock = socket.tcp()
return setmetatable({
connect = function(_, host, port)
local r, e = sock:connect(host, port)
if not r then return r, e end
sock = ssl.wrap(sock, {mode='client', protocol='tlsv1'})
return sock:dohandshake()
end
}, {
__index = function(t,n)
return function(_, ...)
return sock[n](sock, ...)
end
end
})
end
function sendMessage(subject, body)
local msg = {
headers = {
to = 'Your Target <target email>',
subject = subject
},
body = body
}
local ok, err = smtp.send {
from = '<your email>',
rcpt = '<target email>',
source = smtp.message(msg),
user = 'username',
password = 'password',
server = 'smtp.gmail.com',
port = 465,
create = sslCreate
}
if not ok then
print("Mail send failed", err) -- better error handling required
end
end
The code from Michal Kottman works properly but it fails (for me) when smpt server works on 587 port, using a pretty different way to accept mail to send (according wo what I read). Does anybody faced anything similar? I always obtain "wrong version number" on server working on port 587.