如何在.net核心项目中使用c# Web Api读取pdf,替换其文本和发送邮件



我有一个Angular用户注册表单。我需要将用户注册的详细信息作为pdf附件发送到用户的注册邮件地址。

我已经设置了我的smtp邮件和注册表格也按预期工作。我现在所需要做的就是以上的事情。

string content = System.IO.File.ReadAllText(path);
content.Replace("{{FirstName}}", objUser.FirstName);
content.Replace("{{ContactNumber}}", objUser.Contact);
content.Replace("{{EmailId}}", objUser.EmailId);
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.WriteLine(content);
writer.Flush();
stream.Position = 0;
using (var client = new SmtpClient())
{
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
using (var emailMessage = new MailMessage())
{
System.Net.Mime.ContentType contentType = new 


System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
var coverAttachment = new Attachment(stream, contentType);
coverAttachment.ContentDisposition.FileName = "CoverLetter.pdf";
emailMessage.Attachments.Add(coverAttachment);
emailMessage.To.Add(new MailAddress(value.toemail));
emailMessage.CC.Add(new 
MailAddress("My To Address"));
emailMessage.From = new MailAddress(_configuration["Email:Email"]);
emailMessage.Subject = value.subject;
emailMessage.Body = message;
emailMessage.IsBodyHtml = true;
emailMessage.BodyEncoding = Encoding.UTF8;
emailMessage.SubjectEncoding = Encoding.Default;
emailMessage.Priority = MailPriority.High;
emailMessage.ReplyToList.Add(new MailAddress(_configuration["Email:Email"])

这是实现这一目标的正确方法吗?

最后我得到了解决方案,张贴在这里供任何人将来参考(不得不谷歌很多这连接的东西)。

首先我需要添加itextsharp使用我的nuget包管理器

在我的代码

using iTextSharp.text.pdf;
//To test locally and in my hosting server i used below approach
if (System.Diagnostics.Debugger.IsAttached)
{
path = System.IO.Directory.GetCurrentDirectory()
+ Path.DirectorySeparatorChar.ToString() + "Templates"
+ Path.DirectorySeparatorChar.ToString() + "APPLICATION_FORM.pdf";
}
else
{
path = _hostingEnvironment.WebRootPath
+ Path.DirectorySeparatorChar.ToString() + "Templates"
+ Path.DirectorySeparatorChar.ToString() + "APPLICATION_FORM.pdf";
}
string formFile = path;
//Read your PDF using PDFReader
PdfReader reader = new PdfReader(formFile);
MemoryStream stream = new MemoryStream();
PdfStamper stamper = new PdfStamper(reader, stream);
//This **Acrofields** really plucked my hair a lot, later i realized that when you create pdf file, you have option to add fields in your pdf as **FormField**. And name them as {{FirstName}}(can be any value) to replace with my actual text. Then only itextsharp will recognize those Acrofields.
//Check your are getting this fields while debugging
AcroFields fields = stamper.AcroFields;
//And now you can set your fields or replace text
fields.SetField("{{FirstName}}", objUser.FirstName);
fields.SetField("{{LastName}}", objUser.LastName);
fields.SetField("{{Gender}}", objUser.Gender);
fields.SetField("{{ContactNumber}}", objUser.Contact);
stamper.Writer.CloseStream = false;
stamper.FormFlattening = true;
stamper.Close();
stream.Position = 0;
//And then prepare your mail objects
var client = new SmtpClient();
client.Host = _configuration["GoDaddy:Host"];
client.Port = Convert.ToInt32(_configuration["GoDaddy:Port"]);
var emailMessage = new MailMessage();
//Build your attachment from memory stream
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
var coverAttachment = new Attachment(stream, contentType);
coverAttachment.ContentDisposition.FileName = "APPLICATION_FORM.pdf";
emailMessage.Attachments.Add(coverAttachment);
emailMessage.To.Add(new MailAddress(value.toemail));

emailMessage.CC.Add(new MailAddress(_configuration["GoDaddy:CC"]));
emailMessage.From = new MailAddress(_configuration["GoDaddy:Email"]);
emailMessage.ReplyToList.Add(new MailAddress(_configuration["GoDaddy:Email"]));
emailMessage.Subject = value.subject;
emailMessage.Body = message;
emailMessage.IsBodyHtml = true;
emailMessage.BodyEncoding = Encoding.UTF8;
emailMessage.SubjectEncoding = Encoding.Default;
emailMessage.Priority = MailPriority.High;
//And Finally
client.SendCompleted += (s, e) =>
{
if (reader != null)
reader.Close();
stream.Dispose();
emailMessage.Dispose();
client.Dispose();
};
await client.SendMailAsync(emailMessage);
await Task.FromResult(0);

Phewww……就是这样。我在我的go daddy托管空间进行了测试。它正在起作用。快乐编码:)

相关内容

  • 没有找到相关文章

最新更新