Attaches a file attachment from binary data array to the email message.
[Visual Basic]
Public Sub AddAttachment( _
fileName As String, _
content() As Byte _
) As Attachment
[C#]
public Attachment AddAttachment(
string fileName,
byte[] content
);
[C++]
public: Attachment^ AddAttachment(
String^ fileName,
array<unsigned char>^ content,
);
public function AddAttachment(
fileName : String,
content : Byte[]
) : Attachment;
Parameters
Return Value
Remarks
Example
[Visual Basic, C#, JavaScript] To get the full samples of EASendMail, please refer to Samples section.
[C# - Send Email with Attachment from Windows Store Apps - XAML]
using EASendMail;
using System.Threading.Tasks;
private async Task SendEmail()
{
String Result = "";
try
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Set sender email address, please change it to yours
oMail.From = new MailAddress("test@emailarchitect.net");
// Add recipient email address, please change it to yours
oMail.To.Add(new MailAddress("support@emailarchitect.net"));
// Set email subject and text body
oMail.Subject = "test email from C# XAML project";
oMail.TextBody = "this is a test email with Attachment sent from Windows Store App, do not reply";
// Get eml file from your current application folder
StorageFile f =
await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///assets/logo.png"));
var buffer = await FileIO.ReadBufferAsync(f);
Windows.Storage.Streams.DataReader reader = Windows.Storage.Streams.DataReader.FromBuffer(buffer);
byte[] data = new byte[buffer.Length];
reader.ReadBytes(data);
reader.Dispose();
Attachment oAttachment = oMail.AddAttachment("logo.png", data);
// you can change the Attachment name by
// oAttachment.Name = "mytest.png";
// If you want to send email with embedded image
// Specifies the attachment as an embedded image
// string contentID = "test001@host";
// oAttachment.ContentID = contentID;
// oMail.HtmlBody = "<html><body>this is an <img src=\"cid:" + contentID + "\"> embedded picture.</body></html>";
// Your SMTP server address
SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");
// User and password for ESMTP authentication
oServer.User = "test@emailarchitect.net";
oServer.Password = "testpassword";
// If your SMTP server requires TLS connection on 25 port, please add this line
// oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
// If your SMTP server requires SSL connection on 465 port, please add this line
// oServer.Port = 465;
// oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
await oSmtp.SendMailAsync(oServer, oMail);
Result = "Email was sent successfully!";
}
catch (Exception ep)
{
Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
}
// Display Result by Diaglog box
Windows.UI.Popups.MessageDialog dlg = new
Windows.UI.Popups.MessageDialog(Result);
await dlg.ShowAsync();
}
[VB - Send Email with Attachment from Windows Store Apps - XAML]
Imports EASendMail
Private Async Function SendEmail() As Task
Dim Result As String = ""
Try
Dim oMail As New SmtpMail("TryIt")
Dim oSmtp As New SmtpClient()
' Set sender email address, please change it to yours
oMail.From = New MailAddress("test@emailarchitect.net")
' Add recipient email address, please change it to yours
oMail.To.Add(New MailAddress("support@emailarchitect.net"))
' Set email subject and text body
oMail.Subject = "test email from VB XAML project"
oMail.TextBody = "this is a test email with attachment sent from Windows Store App, do not reply"
' Get eml file from your current application folder
Dim f As StorageFile =
Await StorageFile.GetFileFromApplicationUriAsync(New Uri("ms-appx:///assets/logo.png"))
Dim buffer = Await FileIO.ReadBufferAsync(f)
Dim reader = Windows.Storage.Streams.DataReader.FromBuffer(Buffer)
Dim data(Buffer.length - 1) As Byte
reader.ReadBytes(data)
reader.Dispose()
Dim oAttachment As Attachment = oMail.AddAttachment("logo.png", data)
' you can change the Attachment name by
' oAttachment.Name = "mytest.png"
' If you want to send email with embedded image
' Specifies the attachment as an embedded image
' Dim contentID As String = "test001@host"
' oAttachment.ContentID = contentID
' oMail.HtmlBody = "<html><body>this is an <img src=""cid:" + contentID + """> embedded picture.</body></html>"
' Your SMTP server address
Dim oServer As New SmtpServer("smtp.emailarchitect.net")
' User and password for ESMTP authentication
oServer.User = "test@emailarchitect.net"
oServer.Password = "testpassword"
' If your SMTP server requires TLS connection on 25 port, please add this line
' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
' If your SMTP server requires SSL connection on 465 port, please add this line
' oServer.Port = 465
' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
Await oSmtp.SendMailAsync(oServer, oMail)
Result = "Email was sent successfully!"
Catch ep As Exception
Result = String.Format("Failed to send email with the following error: {0}", ep.Message)
End Try
' Display Result by Diaglog box
Dim dlg As New Windows.UI.Popups.MessageDialog(Result)
Await dlg.ShowAsync()
End Function
[JavaScript - Send Email with Attachment from Windows Store Apps - HTML5]
function send_email_attachments()
{
var result = "";
var oMail = new EASendMail.SmtpMail("TryIt");
// Set sender email address, please change it to yours
oMail.from = new EASendMail.MailAddress("test@emailarchitect.net");
// Add recipient email address, please change it to yours
oMail.to.append(new EASendMail.MailAddress("support@emailarchitect.net"));
// Set email subject and text body
oMail.subject = "test email from JavaScript HTML5 project";
oMail.textBody = "this is a test email with attachment sent from Windows Store Apps, do not reply";
// Your SMTP server address
var oServer = new EASendMail.SmtpServer("smtp.emailarchitect.net");
// User and password for ESMTP authentication
oServer.user = "test@emailarchitect.net";
oServer.password = "testpassword";
// If your SMTP server requires TLS connection on 25 port, please add this line
// oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;
// If your SMTP server requires SSL connection on 465 port, please add this line
// oServer.port = 465;
// oServer.connectType = EASendMail.SmtpConnectType.connectSSLAuto;
var oSmtp = new EASendMail.SmtpClient();
// Get attachment file from your current application folder
Windows.Storage.StorageFile.getFileFromApplicationUriAsync(
new Windows.Foundation.Uri("ms-appx:///images/logo.png"))
.then(function (f) {
return Windows.Storage.FileIO.readBufferAsync(f);
})
.then(function (buffer) {
var reader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
var data = new Array(buffer.length);
reader.readBytes(data);
reader.close();
var oAttachment = oMail.addAttachment("logo.png", data);
// you can change the file name like this
// oAttachment.name = "mytest.png";
// If you want to send email with embedded image
// Specifies the attachment as an embedded image
// var contentID = "test001@host";
// oAttachment.contentID = contentID;
// oMail.htmlBody = "<html><body>this is an <img src=\"cid:" + contentID + "\"> embedded picture.</body></html>";
return oSmtp.sendMailAsync( oServer, oMail )
})
.done(function () {
result = "Email was sent successfully!";
(new Windows.UI.Popups.MessageDialog(result, "Success")).showAsync();
},
// error handler
function (e) {
// because javascript exception only gives the stack trace messages, but it is not
// real description of exception, so we give a property lastErrorMessage for javascript.
if (oMail.lastErrorMessage != "") {
result = oMail.lastErrorMessage;
}
else if (oSmtp.lastErrorMessage != "") {
result = oSmtp.lastErrorMessage;
}
else {
result = e.message;
}
(new Windows.UI.Popups.MessageDialog(result, "Error Information")).showAsync();
});
}
See Also
SmtpMail.AddAttachmentAsync Method
SmtpMail.ImportHtmlBodyAsync Method
SmtpMail.ImportHtmlAsync Method
Attachment Class