SmtpClient Class: send() works but not sendAsync() - email

I'm writing a simple Windows Form (Visual Studio 2010, .NET 4.0, Windows 2008 32bit). Clicking on a button make send an e-mail.
The function SendMaildButton_Click() sends the mail, and if completed (aSync method), it calls SendCompletedCallback().
Public Class MailForm
Private sc As System.Net.Mail.SmtpClient
Private Sub MailForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sc = New System.Net.Mail.SmtpClient
AddHandler sc.SendCompleted, AddressOf SendCompletedCallback
End Sub
Private Sub SendCompletedCallback(ByVal sender As Object, _
ByVal e As AsyncCompletedEventArgs)
' [...]
End Sub
Private Sub SendMaildButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SendButton.Click
'sc instance of SmtpClient already created on OnLoad form event
' [sc.Host, sc.Port, sc.Credentials stuff ...]
Dim message As MailMessage
' [message filling]
' [relevant part : Send or SendAsync]
' freedom...
message.Dispose()
End Sub
End Class
This works perflectly if the sending code is :
sc.Send()
Event fakeSMTPServer (a dummy smtp server) or Gmail get it. When I rather use :
Dim userState As String = "plap" 'message.GetHashCode
AddHandler sc.SendCompleted, AddressOf SendCompletedCallback
sc.SendAsync(message, userState)
It doesn't work... fakeSMTPServer notify a mail reception, but without genuinely retrieving it.
The message error is : "Failure sending mail"
Here's the stacktrace of fakeSMTPServer:
19 janv. 2017 17:15:43 ERROR com.nilhcem.fakesmtp.server.MailSaver -
java.io.EOFException: Pre-mature end of <CRLF>.<CRLF> terminated data
at org.subethamail.smtp.io.DotTerminatedInputStream.read(DotTerminatedInputStream.java:73) ~[fakeSMTP-2.0.jar:na]
at org.subethamail.smtp.io.DotUnstuffingInputStream.read(DotUnstuffingInputStream.java:47) ~[fakeSMTP-2.0.jar:na]
at org.subethamail.smtp.io.DotUnstuffingInputStream.read(DotUnstuffingInputStream.java:76) ~[fakeSMTP-2.0.jar:na]
What dit I miss?

The problem is in here:
message.Dispose()
You're freeing your resources before email is sent. You should free your resources only in the SendCompletedCallback, because right now you simply say: start sending this… oh, no, never mind.

Related

Xamarin, get error sending mail using SmtpClient

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.

Send to mail recipient vbscript not longer working

I used to use below VBscript to send files by mail as attachments to be able to add my signature in the e-mail message.
Since about two weeks the VBscript is showing an error every time I try to send a file. I tried to use normal "send to/mail recipient" and it works fine.
Would you advice how can this be solved?
Code:
Option Explicit
Dim objArgs, OutApp, oNameSpace, oInbox, oEmailItem, olMailItem
Dim a, oAttachments, subjectStr, olFormatHTML
olMailItem = 0
olFormatHTML = 2
Set objArgs = WScript.Arguments 'gets paths of selected files
Set OutApp = CreateObject("Outlook.Application") 'opens Outlook
Set oEmailItem = OutApp.CreateItem(olMailItem) 'opens new email
For a = 0 to objArgs.Count - 1
Set oAttachments = oEmailItem.Attachments.Add(objArgs(a))
subjectStr = subjectStr & Right(objArgs(a),Len(objArgs(a))-(InStrRev(objArgs(a),"\"))) & ", " 'recreates the default Subject e.g. Emailing: file1.doc, file2.xls
Next
If subjectStr = "" then subjectStr = "No Subject "
oEmailItem.Subject = "Emailing: " & Left(subjectStr, (Len(subjectStr)-2))
oEmailItem.BodyFormat = olFormatHTML
oEmailItem.Display
Error message:
Unable to execute - arguments list is too long

Open Outlook from Access to scan Inbox

I look for an email in Outlook using VBA in Access. It doesn't work all the time.
I wonder if I do not declare my variables officially, or do not open Outlook properly.
Also, is there a way to open Outlook, refresh the inbox folder, and then begin scanning?
The error is either one of two things, the remote server you are trying to connect to is not available", or "object not declared" as if the Outlook application was never opened.
Dim ns As Outlook.NameSpace
Dim objOL As Outlook.Application
Dim folder As MAPIFolder
Dim item As Object
Dim msg As MailItem
Dim objAttachments As Outlook.Attachments
Set objOL = CreateObject("Outlook.Application")
'The line below will error out occasionally
' with a remote serve not available...
Set ns = Session.Application.GetNamespace("MAPI")
Set folder = ns.GetDefaultFolder(olFolderInbox)
For Each item In folder.Items
DoEvents
If (item.Class = olMail) And (item.UnRead) Then
' This message has not been read. Display it modally
Set msg = item
'Debug.Print (msg.Subject)
'Debug.Print (msg.SenderEmailAddress)
tempString = CStr(msg.Subject)
If Mid(tempString, 1, 4) = "qqqq" Then
'Debug.Print (tempString)
'create new directory
If updateFlag = 0 Then
'this checks if the directory already exists,
' if it does just skip creating the directory since we do not
' want to override previous saved attached inventory files.
If Dir(strFolderpath, vbDirectory) = "" Then
Createdir True, strFolderpath
End If
End If
updateFlag = 1
'Debug.Print msg.Subject
'instantiate attachments object for each openned email
Set objAttachments = msg.Attachments
lngCount = objAttachments.Count
'if you want to open all the emails uncomment next line
'msg.Display True
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & "\" & strFile
WriteLogFile "File saved from blabla#blbl inbox, file path: " & strFile
' Save the attachment as a file.
objAttachments.item(i).SaveAsFile strFile
Next i
msg.UnRead = False
End If
End If
Next
'close the outlook application.
'objOL.Quit
I tried opening Outlook, then closing it and reopening it, but that doesn't work as well. Is there a more proper way to open Outlook? And how to refresh the inbox once open, wait for refresh to complete, and then scan inbox?

Send email with attachment from any email program

I need to send an email from an MS Access database with an attachment (not an Access object, but a separate file), but not tied to any one email software (Groupwise, Outlook, etc). I have found code to send an email with an attachment using Groupwise and Outlook, and there is the generic DoCmd.SendObject which only appears to support attaching Access objects. Is there a way to send an email from Access with an attachment, regardless of the email client configured on the user's PC?
Rationale: There's complications with software rollout here. The machine I work on has Access 2013 and Outlook 2013 installed. The users of the database are running Access 2010, but when I compile the database into a .accde in 2013, it does not work on 2010. The only way I can get it to work is to compile it on a much older PC also running Access 2010. However, this old PC does not have Outlook and IT won't/can't install Outlook on it. This means I can't compile the database using the Outlook library, as there is no Outlook library on the machine.
Here is code I use to send e-mails using Gmail:
Public Function SendEmailViaGmail(SendTo As String, Optional Subject As String = "", Optional TextBody As String = "", Optional ReplyTo As String = "", Optional AttachedFiles As Variant = "") As String
On Error GoTo send_emailErr
Dim ErrNum As Long
Dim ErrDes As String
SendEmailViaGmail = ""
ErrNum = 0
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = sendusername '
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = sendpassword
.Update
End With
' build email parts
With cdomsg
.To = SendTo
.FROM = sendusername
.Subject = Subject
.TextBody = TextBody & vbCrLf & vbCrLf & vbCrLf & "--" & vbCrLf & "Sent using Marlan Data-Systems"
If IsArray(AttachedFiles) Then
For Each AttachedFile In AttachedFiles
If Len(AttachedFile) > 3 Then .AddAttachment AttachedFile
Next
Else
If Len(AttachedFiles) > 3 Then .AddAttachment AttachedFiles
End If
.send
End With
SendEmailViaGmail = "Done!"
send_emailExit:
Set cdomsg = Nothing
Exit Function
send_emailErr:
ErrNum = Err.Number
ErrDes = Err.Description
Select Case Err.Number
Case -2147220977 'Likely cause, Incorrectly Formatted Email Address, server rejected the Email Format
SendEmailViaGmail = "Please Format the Email Address Correctly."
Case -2147220980 'Likely cause, No Recipient Provided (No Email Address)
SendEmailViaGmail = "Please Provide an Email Address"
Case -2147220960 'Likely cause, SendUsing Configuration Error
SendEmailViaGmail = "SendUsing Configuration Error"
Case -2147220973 'Likely cause, No Internet Connection
SendEmailViaGmail = "Please Check Internet Connection"
Case -2147220975 'Likely cause, Incorrect Password
SendEmailViaGmail = "Please Check Password"
Case Else 'Report Other Errors
SendEmailViaGmail = ""
End Select
SendEmailViaGmail = SendEmailViaGmail & " Error number: " & Err.Number & " Description: " & Err.Description
'If ErrNum = -2147220975 Then
' cdomsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
' Resume
'End If
Resume send_emailExit
End Function
AttachedFiles is a String, or an Array of Strings, representing full paths to file or files that are to be attached to the email.
CDO.message is a Microsoft windows object.
You can replace value of smtpserver to some other mailing service. If you do so, please be sure to modify other parameters as well.
Code is based on code I found on the web.

Python 3.4 email to multiple receivers throws an ERROR

I'm writing a script to send an email to more than one email account, but not able, yet.
It works as it is below, but if I set receivers='xxx#xxx.com','yyy#yyy.com' it won't work, it throws an error:
AttributeError: 'tuple' object has no attribute 'encode'.
How can I set receivers=?
def send_email (out_file):
sender = 'xxx#xxx.com'
receivers = 'xxx#xxx.com'
email_pass = 'aaaa'
filematch=re.findall('NE.*\.txt',out_file.name)
subject = ("NEXXXX_price_update")
message = ("The following file was forwarded to your ftp account %s " %filematch)
msg = 'Subject: %s\n%s' %(subject, message)
try:
smtpObj = smtplib.SMTP_SSL('smtp.gmail.com',0)
smtpObj.login(receivers, email_pass)
smtpObj.sendmail(sender, receivers, msg)
print ("Successfully sent email")
except SMTPException:
print ("email NOT successful")
print(SMTPException.__cause__)
smtpObj.quit()
You assign wrongly
receivers='xxx#xxx.com','yyy#yyy.com'
You suppose to assign as a tuple or list, not sure 100% which.
Give a try:
receivers=('xxx#xxx.com','yyy#yyy.com')
or
receivers=['xxx#xxx.com','yyy#yyy.com']