gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:30 24 4
gpt4 key购买 nike

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

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

来 self 的安卓代码。我对 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);
}
}

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

请提供服务器端代码。

最佳答案

您可以简单地使用 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);
}

关于c# - 在电子邮件正文中发送图像,Android 作为客户端,Asp.net c# 作为服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28296830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com