发送带附件的电子邮件 [英] Send Email with attachment

查看:67
本文介绍了发送带附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想发送带附件的电子邮件。因此,当我附加文件并发送电子邮件时,文件已收到,但没有内容显示该文件显示0KB,当我从邮件下载。



这里的代码是< u> aspx页面



Hi,
i want to send email with attachment. so when i attach a file and sending email the file is received but no content was displaying the file shows 0KB when i download from mail.

here is the code in aspx page

string frommail =" from@mail.com";
               string subject="Demo mail";
               string body="check mail have a attachment";
               if (FileUpload1.HasFile.Equals(true))
               {
                   byte[] fileSize = new byte[FileUpload1.PostedFile.ContentLength];
                   HttpPostedFile uploadedfile = FileUpload1.PostedFile;
                   uploadedfile.InputStream.Read(fileSize, 0, (int)FileUpload1.PostedFile.ContentLength);
                    string str = txtto.Text.Replace(',',';');
                    emailaddress = str.Split(';');
                   foreach (string tomail in emailaddress)
                   {
                       if (tomail != "")
                       {
                           Boolean mail = _objsendmail.Sendmailwithattachment(frommail,tomail,subject,body,uploadedfile,fileSize);
                           if (mail == true)
                           {
                               Label1.Text = "mail sent successfully";
                               txtto.Text = "";
                               txtsubject.Text = "";
                               MailEditor.Value = "";
                           }
                       }
                   }
               }







代码




code in

Sendmailwithattachment function







public Boolean Sendmailwithattachment(string from, string to, string subject, string body, HttpPostedFile uploadedfile, byte[] fileSize)
       {

           MailMessage message = new MailMessage();
           message.From = new MailAddress(from);
           string str = to.Replace('','', '';'');
           emailaddress = str.Split('';'');

           foreach (string toaddress in emailaddress)
           {
               if (toaddress != "")
               {
                   message.To.Add(toaddress);
                   message.Subject = subject;
                   message.Body = body;
                   message.Attachments.Add(new Attachment(uploadedfile.InputStream,uploadedfile.FileName));
                   message.IsBodyHtml = true;
                   SmtpClient emails = new SmtpClient("10.0.0.1");
                   emails.Send(message);
                   return true;
               }
           }
           return false;
       }





代码有什么问题我如何在这些函数中传递我的附件文件。



请帮忙。



谢谢,

parithi



what is wrong with the code how can i pass my attach file in these function.

please help.

thanks,
parithi

推荐答案

我会改变这一行:

I would change the this line:
message.Attachments.Add(new Attachment(uploadedfile.InputStream,uploadedfile.FileName));





到此:





to this:

message.Attachments.Add(new MailAttachment(uploadedfile.FileName));





看看能得到什么。



See what that gets you.


您需要创建附件类的对象。可以将 Attachment 类的对象添加到 MailMessage 类中。试试这个:

You need to create the object of Attachment Class. The object of Attachment class can be added to MailMessage Class. Try this:
SmtpClient smtpClient = null;
MailMessage smtpMail = null;
Attachment attachment = null;
try
{
    smtpMail = new MailMessage(From, To);
    smtpMail.Subject = Subject;
    smtpMail.Body = Body;
    smtpMail.IsBodyHtml = IsBodyHTML;
    attachment = new Attachment(file.Trim()); //file : Absolute path of your file
    smtpMail.Attachments.Add(attachment); // Adding attachment "System.Net.Mail"
    smtpClient = new SmtpClient();
    smtpClient.Host = "MailSettings-Host"; //Here you need to pass the host address
    smtpClient.Port = "MailSettings-Port"; //Here you need to pass the host port
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Send(smtpMail);
    Response.Write("Mail sent!!");
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}
finally
{
    attachment = null;
    smtpClient = null;
    smtpMail = null;
}







-Amit




-Amit


I创建了一个保存附件的文件夹。这样,现在可以通过Server.MapPath(filepath)函数轻松获取文件的路径,然后将附件轻松发送给客户端。发送文件后,它将使用File.Delete(文件路径)功能从文件夹中删除。
I have created a folder where my attachment will be saved. so that now the path of the file is easily get by Server.MapPath("filepath") function and then the attachment easily send to client. after sending the file it will be deleted from the folder using File.Delete(filepath) function.


这篇关于发送带附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆