I am trying to write a job for sending mail through SMTPClient. I am using gmail server and I am using credentials of my gmail account that gmail gives us for sending email through a program.
I am using these credentials in my dotNet Application and they are working fine. email is being sent.
BUT
Now I am working in X++ and have created a job for sending email and i am using same credentials. But I am getting an error on the line with code:
mailClient.Send(mailMessage);
the error is: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. v2sm17359467wmd.24 - gsmtp
What is the issue and what is the possible solution ??
my complete code for the job is as following:
static void EmailThroughSMTPExperiment(Args _args)
{
// Set these variables.
str sender = '2011cs163@gmail.com';
str recipient = 'muhammad.g@coh.com';
str cc = 'muhammad.gulfam@coh.com';
str subject = 'Test';
str body = 'Testing';
str fileName = @'C:\test.txt';
str pwd;
List toList;
List ccList;
ListEnumerator le;
Set permissionSet;
System.Exception e;
str mailServer;
int mailServerPort;
System.Net.Mail.SmtpClient mailClient;
System.Net.Mail.MailMessage mailMessage;
System.Net.Mail.MailAddress mailFrom;
System.Net.Mail.MailAddress mailTo;
System.Net.Mail.MailAddressCollection mailToCollection;
System.Net.Mail.MailAddressCollection mailCCCollection;
System.Net.Mail.AttachmentCollection mailAttachementCollection;
System.Net.Mail.Attachment mailAttachment;
SysEmailParameters parameters = SysEmailParameters::find();
;
try
{
toList = strSplit(recipient, ';');
ccList = strSplit(cc, ';');
permissionSet = new Set(Types::Class);
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(filename, 'rw'));
CodeAccessPermission::assertMultiple(permissionSet);
mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
mailClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort);
le = toList.getEnumerator();
le.moveNext();
mailFrom = new System.Net.Mail.MailAddress(sender);
mailTo = new System.Net.Mail.MailAddress(strLTrim(strRTrim(le.current())));
mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);
mailToCollection = mailMessage.get_To();
while (le.moveNext())
{
mailToCollection.Add(strLTrim(strRTrim(le.current())));
}
le = ccList.getEnumerator();
mailCCCollection = mailMessage.get_CC();
while (le.moveNext())
{
mailCCCollection.Add(strLTrim(strRTrim(le.current())));
}
mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
mailMessage.set_Subject(subject);
mailMessage.set_Body(body);
mailAttachementCollection = mailMessage.get_Attachments();
mailAttachment = new System.Net.Mail.Attachment(fileName);
mailAttachementCollection.Add(mailAttachment);
//mailClient.set_EnableSsl=true;
pwd=SysEmaiLParameters::password();
mailClient.set_Credentials(New System.Net.NetworkCredential(parameters.SMTPUserName, pwd));
mailClient.Send(mailMessage);
mailMessage.Dispose();
CodeAccessPermission::revertAssert();
info("Email sent.");
}
catch (Exception::CLRError)
{
e = ClrInterop::getLastException();
while (e)
{
info(e.get_Message());
e = e.get_InnerException();
}
CodeAccessPermission::revertAssert();
}
}