How to request the operating system to send email with a specific subject, body and attachment? The OS should launch the default email client with the fields pre-loaded?
Specifically what API functions are available in C#.net?
How to request the operating system to send email with a specific subject, body and attachment? The OS should launch the default email client with the fields pre-loaded?
Specifically what API functions are available in C#.net?
Since you want the outlook to send the email, substitute the port number and host with that of the outlook.
To Open Outlook with pre-loaded fields:
using System.Diagnostics;
// ...
Process.Start(String.Format("mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", address, subject, cc, bcc, body))
To send the Email directly :
using System.Net;
using System.Net.Mail;
// ...
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient {
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using(var message = new MailMessage(fromAddress, toAddress) {
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
This is a simple adapter for system SmtpClient
that will simplify yours tests.
public class SmtpClientAdapter : ISmtpClient
{
private bool disposed;
private SmtpClient smtpClient = new SmtpClient();
public SendResult Send(MailMessage message)
{
if (disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
try
{
smtpClient.Send(message);
return SendResult.Successful;
}
catch (Exception e)
{
return new SendResult(false, e.Message);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing)
{
if (smtpClient != null)
{
smtpClient.Dispose();
smtpClient = null;
}
}
disposed = true;
}
}
public class SendResult : SimpleResult
{
public SendResult(bool success, string message)
: base(success, message)
{
}
public static SendResult Successful
{
get
{
return new SendResult(true, string.Empty);
}
}
}
Usage
var emailFrom = new MailAddress(sender);
var emailTo = new MailAddress(recipient);
var mailMessage = new MailMessage(emailFrom, emailTo)
{
Subject = "Subject ",
Body = "Body"
};
using (var client = new SmtpClientAdapter())
{
return client.Send(mailMessage);
}
Here is a simple way to do it,
public void SendEmail(string address, string subject, string message)
{
string email = "yrshaikh.mail@gmail.com";
string password = "put-your-GMAIL-password-here";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
Source : Link