How To Send Mail With Attachments

In this article, we are going to  learn how to send emails with attachments in asp.net

First of all, we make a one console app in Asp.net Core

Step 1: First of all create a new project in “Visual Studio”

Step 2: Select Console Application From Menu

Step 3: Give the project name “EmailWithAttachments” and click on “Next” But0ton

Step 4: And Select Asp.net Core here I am using  .Net core 3.1

Note: Here we make a console app for easily understand you can use further code in ASP.net And also you can use in ASP.Net Core

Now We are going to write Code

Step 5: Now we get below type of workspace in the visual studio

Now add Below Code in the “Program.cs” file

using (MailMessage mm = new MailMessage("Sender Mail", "Reciver Mail"))
           {
               mm.Subject = "Test";
               mm.Body = "Test With Atachment";
               Attachment data = new Attachment("C:/Users/pc3/Desktop/MailTransfer/Attachment/D2E5FA61-7DBB-411E-984E-2B3F66A374E4.jpg", MediaTypeNames.Application.Octet);
               mm.Attachments.Add(data);
               mm.IsBodyHtml = false;
               using (SmtpClient smtp = new SmtpClient())
               {
                   smtp.Host = "smtp.gmail.com";
                   smtp.EnableSsl = true;
                   NetworkCredential NetworkCred = new NetworkCredential("Write Sender Email Here", "Write Sender Email Password here");
                   smtp.UseDefaultCredentials = true;
                   smtp.Credentials = NetworkCred;
                   smtp.Port = 587;
                   smtp.Send(mm);
               }
           }

Like This : 

“You can use upper code in your MVC project and also use it in the .Net Core project this code works in both”

NOTE: Don’t Forget to Enter the following using in “Program.cs” File

using System.Net;
using System.Net.Mail;
using System.Net.Mime;

Step 6: Now Run Your Console App and see in your Email Inbox


I hope You guys find the solution to your problem from this article 

Plz share the blog with others and be a helpful ????

NOTE: in the video, You can see I bind my Email Credential from the AssConfig.js file for privacy issue so please appreciate and                     thanks you can write your credential in function also

Thanks,

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories