SoFunction
Updated on 2025-03-08

C# uses classes to implement email sending

Some SMTP servers require that the client be authenticated before sending an email on behalf of the client. When thisSmtpClientWhen the object should authenticate using the default credentials of the currently logged in user (if required by the server), set this property to true. For client applications, this is a behavior that is required in most cases. You can also specify credential information using application and computer configuration files. For more information, seeElement (network settings)

If the UseDefaultCredentials property is set to false, then the     will be     when connecting to the serverCredentialsThe value set in the attribute is used as the credentials. If the UseDefaultCredentials property is set to false and has not been set yetCredentialsproperty, send the email to the server anonymously. The default value of UseDefaultCredentials is false.

If credentials are provided for basic authentication, the credentials will be sent to the server in plaintext. This can create security issues because credentials can be seen by others and exploited. However, sending emails to 163's mailbox must bring the sender's credential information, otherwise the email will not be sent out.

SmtpClient can also be created like this:SmtpClient client=new SmtpClient(smtp_server), because the default port of the mail sending server is 25.

1. QQ mailbox sends email to 163 mailbox

private static void TimeEvent(object source, ElapsedEventArgs e)
{
    string smtp_server="";
    int port = 25;
    string mail_from = "***from_mail_username***@";
    string sender = "***from_mail_username***";
    string mail_to = "***to_mail_username***@";
    string receiver = "***to_mail_username***@";
    string subject = "Title Test"
    string body = "Content Test";
    try
    {
    SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
    }
    catch(Exception ex)
    {
    ();
    }
}

public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
    MailAddress from = new MailAddress(mail_from, sender);
    MailAddress to = new MailAddress(mail_to, receiver);
    MailMessage message = new MailMessage(from, to);
     = Encoding.UTF8;
     = true;
     = subject;
     = body;

    SmtpClient client = new SmtpClient(smtp_server, port);   
     = new NetworkCredential("***from_mail_username***@", "***pwd***");
    (message);
}

2. 163 Email Send email to 163 Email

private static void TimeEvent(object source, ElapsedEventArgs e)
{
    string smtp_server="smtp.";
    int port = 25;
    string mail_from = "***from_mail_username***@";
    string sender = "***from_mail_username***";
    string mail_to = "***to_mail_username***@";
    string receiver = "***to_mail_username***";
    string subject = "Title Test"
    string body = "Content Test";
    try
    {
    SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
    }
    catch(Exception ex)
    {
    ();
    }
}

public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
    MailAddress from = new MailAddress(mail_from, sender);
    MailAddress to = new MailAddress(mail_to, receiver);
    MailMessage message = new MailMessage(from, to);
     = Encoding.UTF8;
     = true;
     = subject;
     = body;

    SmtpClient client = new SmtpClient(smtp_server);   
     = new NetworkCredential("***from_mail_username***@", "***pwd***");
    (message);
}

3. 163 email sends email to QQ email

private static void TimeEvent(object source, ElapsedEventArgs e)
{
    string smtp_server="smtp.";
    int port = 25;
    string mail_from = "***from_mail_username***@";
    string sender = "***from_mail_username***";
    string mail_to = "***to_mail_username***@";
    string receiver = "***to_mail_username***";
    string subject = "Title Test"
    string body = "Content Test";
    try
    {
    SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
    }
    catch(Exception ex)
    {
    ();
    }
}

public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
    MailAddress from = new MailAddress(mail_from, sender);
    MailAddress to = new MailAddress(mail_to, receiver);
    MailMessage message = new MailMessage(from, to);
     = Encoding.UTF8;
     = true;
     = subject;
     = body;

    SmtpClient client = new SmtpClient(smtp_server);   
     = new NetworkCredential("***from_mail_username***@", "***pwd***");
    (message);
}

4. Send emails to QQ mailbox

private static void TimeEvent(object source, ElapsedEventArgs e)
{
    string smtp_server="";
    int port = 25;
    string mail_from = "***from_mail_username***@";
    string sender = "***from_mail_username***";
    string mail_to = "***to_mail_username***@";
    string receiver = "***to_mail_username***";
    string subject = "Title Test"
    string body = "Content Test";
    try
    {
    SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
    }
    catch(Exception ex)
    {
    ();
    }
}

public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
{
    MailAddress from = new MailAddress(mail_from, sender);
    MailAddress to = new MailAddress(mail_to, receiver);
    MailMessage message = new MailMessage(from, to);
     = Encoding.UTF8;
     = true;
     = subject;
     = body;

    SmtpClient client = new SmtpClient(smtp_server);   
     = new NetworkCredential("***from_mail_username***@", "***pwd***");
    (message);
}

This is all about this article about C# using classes to implement email sending. I hope it will be helpful to everyone's learning and I hope everyone will support me more.