Ram Singh Ram Singh - 2 months ago 9x
ASP.NET (C#) Question

how to attach a file in email using shared hosting server?

I want to attach a file dynamically and send it through mail. So can someone please let me know how can i send it with attachment.


You can use this code.

using System.Net.Mail;

private void SendEmail()
    MailMessage message = new MailMessage();
    message.From = new MailAddress("YourEmailAddress@domain.com");
    message.To.Add(new MailAddress("Recipient@domain.com"));
    message.Subject = "Subject";
    message.Body = "Email Message Body";

    // Add attachment
    string attachmentPath = Server.MapPath("~/AttachmentPath.jpg");
    message.Attachments.Add(new Attachment(attachmentPath));

    // Connect to GoDaddy SMTP
    SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net");
    smtp.Credentials = new System.Net.NetworkCredential("Username","Password");
    smtp.Port = 25;
    smtp.EnableSsl = false;

    // Send the message