这是我的代码,请帮助我。
[HttpGet]
public string sendmail( string email,string body, string attach)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(email);
mail.To.Add("sachinrameshgiri@gmail.com");
mail.Subject = "Test Mail";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(attach );
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("sachingiri.bca@gmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return "success";
}
使用jquery ajax从HTML页面调用它 $(document).ready(function(){
$("#btn1").click(function () {
$.getJSON("api/person/sendmail", { email: "sachingiri.bca@gmail.com", body: "hi this is sachin giri !!!A software developer",attach:" "}, function (data) ....
这适用于发送邮件,但是html输入类型=“文件”不会返回上传路径,只返回文件名...所以附件不起作用。