0

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?

Raj
  • 3,051
  • 6
  • 39
  • 57

4 Answers4

4

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);
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
user1071979
  • 1,701
  • 2
  • 12
  • 25
1

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);
        }
user854301
  • 5,383
  • 3
  • 28
  • 37
1

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

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
0

A operating system API function for this isn't avaiable.

Raj
  • 3,051
  • 6
  • 39
  • 57