SoFunction
Updated on 2025-03-10

.net SMTP sends emails with attachments example


public static void sendEmail(string toAddress, string emailbody)
{
var fromAddress = ["EmailAddress"];
string fromPassword = ["EmailPassword"].ToString();
const string subject = "Job Recommendation";


var smtp = new SmtpClient
{
Host = ["SmtpServer"].ToString(),
Port = (["SmtpPort"]),
EnableSsl = true,
DeliveryMethod = ,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress, subject, (emailbody)))
{
(message);
}
}

<add key="EmailAddress" value="**********@"/>//Email Address
<add key="EmailPassword" value="*********"/> //Emial PWD
<add key="SmtpServer" value=""/>
<add key="SmtpPort" value="587"/>

<--With attachment version->

var fromAddress = "allenyinj@";
string fromPassword = "yj1989120";
const string subject = "CV";


var smtp = new SmtpClient
{
Host = "",
Port = 587,
EnableSsl = true,
DeliveryMethod = ,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
MailMessage email=new MailMessage(fromAddress, "@");
= "INLINE attachment TEST";
= true;
string attachmentPath = "C:\\";
Attachment inline = new Attachment(attachmentPath);
= true;
= ;
// = "1";
// = "image/png";
= (attachmentPath);
(inline);
= "test";
(email);

();

//If there is no path, use Stream

Attachment letter = new Attachment(, );
= true;
= ;
// = "1";
= ;
= ();
= ();

//If you want to send it through anonymous mail

var smtp = new SmtpClient
{
Host = "serverName",
Port = 25, //Anonymous sending port
EnableSsl = false,
DeliveryMethod = ,
UseDefaultCredentials = false,
//Credentials = new NetworkCredential(fromAddress, fromPassword)
};