Я читаю почту из gmail а затем вставляю ее в базу данных вместе с телом но в теге тела она показывает HTML контент вместе с данными тела
I am Reading Mail from Gmail and then Insert it Into sql server database along with body but In body tag it is inserting html content along with body data
например мой тег тела содержимого электронной почты выглядит следующим образом
Your password changed Hi my, The password for your Google Account memyid2016@gmail.com was recently changed. Don't recognize this activity? Click here for more information on how to recover your account. The Google Accounts team
но в базе данных это выглядит следующим образом
<html lang=en><head><meta content="date=no" name="format-detection"><meta content="email=no" name="format-detection"><style>.awl a {color: #FFFFFF; text-decoration: none;}.abml a {color: #000000; font-family: Roboto-Medium,Helvetica,Arial,sans-serif; font-weight: bold; text-decoration: none;}.adgl a {color: rgba(0, 0, 0, 0.87); text-decoration: none;}.afal a {color: #b0b0b0; text-decoration: none;}@media screen and (min-width: 600px) {.v2sp {padding: 6px 30px 0px;} .v2rsp {padding: 0px 10px;}}</style></head><body bgcolor="#FFFFFF" style="margin: 0; padding: 0;"><table border=0 cellpadding=0 cellspacing=0 height="100%" style="min-width: 348px;" width="100%"><Tbody><tr height=32px></tr><tr align=center><td width=32px></td><td><table border=0 cellpadding=0 cellspacing=0 style="max-width: 600px;"><Tbody><tr><td><table border=0 cellpadding=0 cellspacing=0 width="100%"><Tbody><tr><td align=left><img height=32 src="https://www.gstatic.com/accountalerts/email/googlelogo_color_188x64dp.png" style="display: block; width: 92px; height: 32px;" width=92></td><td align=right><img height=32 src="https://www.gstatic.com/accountalerts/email/shield.png" style="display: block; width: 32px; height: 32px;" width=32></td></tr></Tbody></table></td></tr><tr height=16></tr><tr><td><table bgcolor="#D94235" border=0 cellpadding=0 cellspacing=0 style="min-width: 332px; max-width: 600px; border: 1px solid #F0F0F0; border-bottom: 0; border-top-left-radius: 3px; border-top-right-radius: 3px;" width="100%"><Tbody><tr><td colspan=3 height=72px></td></tr><tr><td width=32px></td><td style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 24px; color: #FFFFFF; line-height: 1.25; min-width: 300px;">Your password changed</td><td width=32px></td></tr><tr><td colspan=3 height=18px></td></tr></Tbody></table></td></tr><tr><td><table bgcolor="#FAFAFA" border=0 cellpadding=0 cellspacing=0 style="min-width: 332px; max-width: 600px; border: 1px solid #F0F0F0; border-bottom: 1px solid #C0C0C0; border-top: 0; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px;" width="100%"><Tbody><tr height=16px><td rowspan=3 width=32px></td><td></td><td rowspan=3 width=32px></td></tr><tr><td><table border=0 cellpadding=0 cellspacing=0 style="min-width: 300px;"><Tbody><tr><td style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 13px; color: #202020; line-height: 1.5;padding-bottom: 4px;">Hi my,</td></tr><tr><td style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 13px; color: #202020; line-height: 1.5;padding: 4px 0;">The password for your Google Account <a>memyid2016@gmail.com</a> was recently changed.<br><br>Don't recognize this activity?<br>Click <a data-meta-key="recover-account" href="https://accounts.google.com/RecoverAccount?fpOnly=1&source=ancppe&Email=memyid2016@gmail.com" style="text-decoration: none; color: #4285F4;" target="_blank">here</a> for more information on how to recover your account.</td></tr><tr><td style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 13px; color: #202020; line-height: 1.5; padding-top: 28px;">The Google Accounts team</td></tr><tr height=16px></tr><tr><td><table style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 12px; color: #B9B9B9; line-height: 1.5;"><Tbody><tr><td>This email can't receive replies. For more information, visit the <a data-meta-key=help href="https://support.google.com/accounts/answer/1628875" style="text-decoration: none; color: #4285F4;" target="_blank">Google Accounts Help Center</a>.</td></tr></Tbody></table></td></tr></Tbody></table></td></tr><tr height=32px></tr></Tbody></table></td></tr><tr height=16></tr><tr><td style="max-width: 600px; font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 10px; color: #BCBCBC; line-height: 1.5;"></td></tr><tr><td><table style="font-family: Roboto-Regular,Helvetica,Arial,sans-serif; font-size: 10px; color: #666666; line-height: 18px; padding-bottom: 10px"><Tbody><tr><td>You received this mandatory email service announcement to update you about important changes to your Google product or account.</td></tr><tr height=6px></tr><tr><td><div style="direction: ltr; text-align: left">© 2018 Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, USA</div><div style="display: none !important; mso-hide:all; max-height:0px; max-width:0px;">et:1</div></td></tr></Tbody></table></td></tr></Tbody></table></td><td width=32px></td></tr><tr height=32px></tr></Tbody></table><img height=1 src="https://notifications.googleapis.com/email/a.gif?t=AFG8qyXeAbLKIeVj-ZuQi4EX_keEmYi7SYKydF9LSgpMZSEVGNNEAeZved2etWJ2CMAYyByKS4uA5Z7AA0gjsJkxBuilM31JjkPk5iKHrkq_bY7d804fjLI5EOHQAY351j2ewxCDNwUmKJfFf2XoRZPbh3XGxcmi9aji_2wgoNMOPdAZkwYA06qe6_5yoaQzXL1ANykZ60RP29mE915LuwsMqUKg18_T6Ou9STw44sEZ" width=1></body></html>
Я не хочу, чтобы HTML-содержимое, я просто хочу сообщение
Что я уже пробовал:
Создал aspx-страницу входа и один GridView для отображения содержимого писем
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EmailSolution.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table class="style1"> <tr> <td colspan="3"> <h2 style="text-align: center">Welcome To Gmail</h2></td> </tr> <tr> <td> <asp:Label ID="Label1" runat="server" Text="Gmail ID :"></asp:Label> </td> <td> <asp:TextBox ID="txt_email" runat="server" Width="241px"></asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt_email" ErrorMessage="Invalid Mail Format" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> </td> </tr> <tr> <td> <asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label> </td> <td> <asp:TextBox ID="txt_password" runat="server" TextMode="Password" Width="239px"></asp:TextBox> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_password" ErrorMessage="*"></asp:RequiredFieldValidator> </td> </tr> <tr> <td colspan="3" style="text-align: center"> <asp:Button ID="Button1" runat="server" CausesValidation="False" onclick="Button1_Click" style="text-align: center" Text="Login" Width="72px" /> </td> </tr> </table> <asp:GridView ID="gvEmails" runat="server" AllowPaging="true" PageSize="5" OnPageIndexChanging="gvEmails_PageIndexChanging"></asp:GridView> </div> </form> </body> </html>
и файлы .cs следующим образом
using OpenPop.Mime; using OpenPop.Pop3; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EmailSolution { public partial class WebForm1 : System.Web.UI.Page { static string ConnectionString = ConfigurationManager.ConnectionStrings["MailConnection"].ConnectionString; protected List<Email> Emails { get { return (List<Email>)ViewState["Emails"]; } set { ViewState["Emails"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } private void Read_Emails(string Email, string Password) { Pop3Client pop3Client; if (Session["Pop3Client"] == null) { pop3Client = new Pop3Client(); pop3Client.Connect("pop.gmail.com", 995, true); //pop3Client.Authenticate("email@gmail.com", "123456", AuthenticationMethod.TryBoth); pop3Client.Authenticate(Email, Password); //pop3Client.Authenticate("memyid2016@gmail.com", "Qaz1234567890"); Session["Pop3Client"] = pop3Client; } else { pop3Client = (Pop3Client)Session["Pop3Client"]; } int count = pop3Client.GetMessageCount(); this.Emails = new List<Email>(); int counter = 0; for (int i = count; i >= 1; i--) { Message message = pop3Client.GetMessage(i); Email email = new Email() { MessageNumber = i, Subject = message.Headers.Subject, DateSent = message.Headers.DateSent, From = string.Format("<a href = 'mailto:{1}'>{0}</a>", message.Headers.From.DisplayName, message.Headers.From.Address), }; MessagePart body = message.FindFirstHtmlVersion(); if (body != null) { email.Body = body.GetBodyAsText(); } else { body = message.FindFirstPlainTextVersion(); if (body != null) { email.Body = body.GetBodyAsText(); } } //List<MessagePart> attachments = message.FindAllAttachments(); //foreach (MessagePart attachment in attachments) //{ // email.Attachments.Add(new Attachment // { // FileName = attachment.FileName, // ContentType = attachment.ContentType.MediaType, // Content = attachment.Body // }); //} this.Emails.Add(email); counter++; if (counter > count) { break; } } Insert(Emails); gvEmails.DataSource = this.Emails; gvEmails.DataBind(); } protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Repeater rptAttachments = (e.Row.FindControl("rptAttachments") as Repeater); List<Attachment> attachments = this.Emails.Where(email => email.MessageNumber == Convert.ToInt32(gvEmails.DataKeys[e.Row.RowIndex].Value)).FirstOrDefault().Attachments; rptAttachments.DataSource = attachments; rptAttachments.DataBind(); } } protected void Download(object sender, EventArgs e) { LinkButton lnkAttachment = (sender as LinkButton); GridViewRow row = (lnkAttachment.Parent.Parent.NamingContainer as GridViewRow); List<Attachment> attachments = this.Emails.Where(email => email.MessageNumber == Convert.ToInt32(gvEmails.DataKeys[row.RowIndex].Value)).FirstOrDefault().Attachments; Attachment attachment = attachments.Where(a => a.FileName == lnkAttachment.Text).FirstOrDefault(); Response.AddHeader("content-disposition", "attachment;filename=" + attachment.FileName); Response.ContentType = attachment.ContentType; Response.BinaryWrite(attachment.Content); Response.End(); } [Serializable] public class Email { public Email() { this.Attachments = new List<Attachment>(); } public int MessageNumber { get; set; } public string From { get; set; } public string Subject { get; set; } public string Body { get; set; } public DateTime DateSent { get; set; } public List<Attachment> Attachments { get; set; } } [Serializable] public class Attachment { public string FileName { get; set; } public string ContentType { get; set; } public byte[] Content { get; set; } } protected void Button1_Click(object sender, EventArgs e) { Session["email"] = txt_email.Text; Session["pwd"] = txt_password.Text; Read_Emails(txt_email.Text, txt_password.Text); } public void Insert(List<Email> emails) { SqlConnection con = new SqlConnection(ConnectionString); string userId; string MFrom; string MSubject; string MBody; DateTime MailDate; int MessageNumber; DateTime drvalues; string finalstring; if (emails.Count > 0) { bool flag = false; foreach (var iteam in emails) { //MContentType = iteam.From.ToString(); userId = Session["email"].ToString(); MFrom = iteam.From.ToString(); MSubject = iteam.Subject.ToString(); MBody = iteam.Body.ToString(); MailDate = iteam.DateSent; string str; MessageNumber = Convert.ToInt32(iteam.MessageNumber); if (flag == false) { flag = true; SqlCommand cmd1 = new SqlCommand("select max(mDateSent) as mDateSent from tblNewMail where userId='" + userId + "'", con); con.Open(); SqlDataReader dr = cmd1.ExecuteReader(); while (dr.Read()) { string mstr = dr["mDateSent"].ToString(); if (mstr != null && mstr != "") { ViewState["drvalues"] = dr["mDateSent"].ToString(); } } con.Close(); } //str = ; if (ViewState["drvalues"]== null) { drvalues = Convert.ToDateTime(ViewState["drvalues"]); if (drvalues < MailDate) { ViewState["drvalues"] = null; string input = MFrom; int index = input.IndexOf(">"); if (index > 0) input = input.Substring(0, index); int index1 = input.IndexOf('<'); finalstring = input.Substring(index1 + 1); SqlCommand cmd = new SqlCommand("Sp_InsertNewMailData", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@MessageNumber", MessageNumber); cmd.Parameters.AddWithValue("@MFrom", MFrom); cmd.Parameters.AddWithValue("@MSubject", MSubject); cmd.Parameters.AddWithValue("@MBody", MBody); cmd.Parameters.AddWithValue("@mDateSent", MailDate); cmd.Parameters.AddWithValue("@userId", userId); con.Close(); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } else { drvalues = Convert.ToDateTime(ViewState["drvalues"]); if (drvalues < MailDate) { ViewState["drvalues"] = null; string input = MFrom; int index = input.IndexOf(">"); if (index > 0) input = input.Substring(0, index); int index1 = input.IndexOf('<'); finalstring = input.Substring(index1 + 1); SqlCommand cmd = new SqlCommand("Sp_InsertNewMailData", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@MessageNumber", MessageNumber); cmd.Parameters.AddWithValue("@MFrom", MFrom); cmd.Parameters.AddWithValue("@MSubject", MSubject); cmd.Parameters.AddWithValue("@MBody", MBody); cmd.Parameters.AddWithValue("@mDateSent", MailDate); cmd.Parameters.AddWithValue("@userId", userId); con.Close(); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } } } protected void gvEmails_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvEmails.PageIndex = e.NewPageIndex; gvEmails.DataSource = this.Emails; gvEmails.DataBind(); } } }