0

我想动态附加文件并通过邮件发送。所以有人可以让我知道如何发送附件。

4

2 回答 2

0

您可以尝试使用此代码

                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("...");/:Adjust your adress
                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail";
                mail.Body = "mail with attachment";

                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment("your attachment file");
                mail.Attachments.Add(attachment);

                SmtpServer.Port = ..; //Replace with your port number
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
于 2012-09-03T09:06:38.587 回答
0

您可以使用此代码。

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
    smtp.Send(message);
}
于 2012-09-03T09:07:19.290 回答