在电子邮件正文中发送图像,在客户端发送Android,在服务器端发送Asp.net c#

时间:2015-02-03 10:38:40

标签: c# android asp.net server-side

我正在开发一个Android应用程序,我必须在其中发送用户的图像

在电子邮件正文中。我将字节数组形式的图像数据发送到服务器端

来自我的Android代码。我对asp.net和C#,

的了解不多

所以有人可以告诉我该怎么做?

虽然我管理了在电子邮件正文中发送文本的代码,但这里是代码。

protected void Page_Load(object sender, EventArgs e)
    {   
     try
        {

             var c = HttpContext.Current;
              var emailID = c.Request["emailID"];
              var passwordToSend = c.Request["password"];
            SmtpClient client = new SmtpClient("smtpserver", portno);
            client.EnableSsl = false;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("########", "########");

            MailMessage messageBody = new MailMessage("#########", emailID);
            messageBody.Subject = "FORGOTTEN PASSWORD";
            messageBody.Body = passwordToSend;
            messageBody.IsBodyHtml = true;
            client.Send(messageBody);
            Response.Write("$success$");
           // return "true";
        }
        catch (Exception ex)
        {
             Response.Write("$fail$");
            Response.Write(ex.Message);
        }
    }

但是我无法理解如何在页面加载事件中在电子邮件正文中发送图像。

请为服务器端提供代码。

2 个答案:

答案 0 :(得分:0)

您只需使用cid链接资源即可。

static void SendMailWithEmbededImages()
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");

//then we create the Html part
//to embed images, we need to use the prefix 'cid' in the img src value
//the cid value will map to the Content-Id of a Linked resource.
//thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");

//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource( "c:\\temp\\logo.gif" );
logo.ContentId = "companylogo";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);

//add the views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);


//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);
}

答案 1 :(得分:0)

试试这个:

                   string outputFile="image path";
                   MailMessage message = new MailMessage();
                   SmtpClient smtp = new SmtpClient();
                    Attachment attachment = new Attachment(outputFile,  MediaTypeNames.Text.Html);
                    ContentDisposition disposition = attachment.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(outputFile);
                    disposition.ModificationDate = File.GetLastWriteTime(outputFile);
                    disposition.ReadDate = File.GetLastAccessTime(outputFile);
                    disposition.FileName = Path.GetFileName(outputFile);
                    disposition.Size = new FileInfo(outputFile).Length;
                    disposition.DispositionType = DispositionTypeNames.Attachment;
                    message.Attachments.Add(attachment);
                    smtp.Send(message);