/// <summary> /// 根据邮件id获取邮件 /// </summary> /// <param name="i">邮件id</param> /// <returns>邮件</returns> private ReceivedMail GetMailByIndex(int i) { if (State == Pop3STATE.CONNECTED) //在可操作状态下才可操作 { SendCommand("RETR " + i, StrmWtr); string ret = ReceiveResponse(StrmRdr, true); if (ret.IndexOf("+OK") == 0) { ReceivedMail receivedMail = new ReceivedMail(i); //转换格式,成可读 ret = MailTextParserUtil.GetReadText(ret); //获取发件人 MailTextParserUtil.GetSender(ret, receivedMail); //获取收件人 MailTextParserUtil.GetReceiver(ret, receivedMail); //获取主题 receivedMail.Subject = MailTextParserUtil.GetSubject(ret); //获取时间 receivedMail.SendDateTime = MailTextParserUtil.GetDateTime(ret); //获取邮件大小 receivedMail.Size = GetMailSizeByIndex(i); //获取主体内容 MailTextParserUtil.GetMailText(ret, receivedMail); return(receivedMail); } return(null); } return(null); }
/// <summary> /// Add a RecievedMail entity to the database corresponding to the given year and the Address /// entity for which this EditAddressViewModel was created. Throws an InvalidOperationException if /// the given year already exists in the database. /// </summary> /// <param name="year">The year to create a RecievedMail entity for in the database.</param> public void AddYear(int year) { if (ReceivedMails.Any(rm => rm.Year == year)) { throw new ArgumentException("Year already exists!"); } if (year < 1970) { throw new ArgumentException("Year too old!"); } if (year > DateTime.Now.Year) { throw new ArgumentException("Year can't be in the future!"); } using (var db = new MailerEntities()) { db.Addresses.Attach(Address); var newRm = new ReceivedMail { Year = year }; Address.ReceivedMails.Add(newRm); ReceivedMails.Add(newRm); db.SaveChangesAsync(); } }
/// <summary> /// 获取收件人信息(直接修改) /// </summary> /// <param name="mail">邮件原始信息</param> /// <param name="receivedMail">邮件</param> public static void GetReceiver(string mail, ReceivedMail receivedMail) { string ret = GetAttributeValue(mail, "To: ", "\n").Replace("\n", ""); if (ret == "") { ret = GetAttributeValue(mail, "to: ", "\n").Replace("\n", ""); } ret = ret.Replace("\"", ""); ret = ret.Replace("<", ""); ret = ret.Replace(">", ""); ret = ret.Replace("“", ""); ret = ret.Replace("”", ""); string[] nameAndAddress = ret.Split(' '); if (nameAndAddress.Length == 1) { receivedMail.ToName = ""; receivedMail.ToAdderss = ret; receivedMail.To = ret; } else { receivedMail.ToName = nameAndAddress[0]; receivedMail.ToAdderss = nameAndAddress[1]; receivedMail.To = nameAndAddress[0]; } }
//邮件选择列表中选择项改变 private void listBox_receivedMails_SelectedIndexChanged(object sender, EventArgs e) { ReceivedMail mail = GetSelectedMail(); if (mail != null) { ShowMailText(mail); } }
public async Task NoSendsGreetingWhenNoBirthdays() { File(fileConfiguration.FilePath, Header(), Employee("Mary", "1982/11/08", "*****@*****.**")); await app.Run(Date("11/09/2020")); ReceivedMail.FromAll(smtpServer) .Should() .BeEmpty(); }
public async Task StoreMail(List<string> to, string mailGuid, ReceivedMail receivedMail) { try { Console.Out.WriteLine("storing the mail...not!"); foreach (var mailbox in to) { await _redisDb.HashSetAsync(mailbox, mailGuid, JsonConvert.SerializeObject(receivedMail)); } } catch (Exception e) { Console.WriteLine(e); } }
public async Task SendBirthday() { using var smtpServer = SimpleSmtpServer.Start(smtpConfiguration.Port); using var notification = new SmtpGreetingsNotification(smtpConfiguration); await notification.SendBirthday(new[] { new EmailInfo("foo", "*****@*****.**"), }); ReceivedMail.FromAll(smtpServer) .Should() .BeEquivalentTo(new ReceivedMail(smtpConfiguration.Sender, "*****@*****.**", "Happy birthday!", "Happy birthday, dear foo!")); }
//删除邮件 private void 除邮件ToolStripMenuItem_Click(object sender, EventArgs e) { // 获取邮件的真实index MessageForm messageForm = new MessageForm("警告", "确定删除该邮件吗?", "删除", "取消"); messageForm.ShowDialog(); if (messageForm.DialogResult == DialogResult.OK) { ReceivedMail mail = GetSelectedMail(); DataService.pop3.DelMail(mail.Id); receivedMails.Remove(mail); //同步更新全局变量 DataService.pop3.User.ReceivedMails = receivedMails; ReverseUpdate(); listBox_receivedMails.Refresh(); } }
public async Task ServerUnreachableDuringSend() { using var smtpServer = SimpleSmtpServer.Start(smtpConfiguration.Port); using var notification = new SmtpGreetingsNotification(smtpConfiguration, new StopServerAfterOneSend(smtpServer)); var ex = await Assert.ThrowsAsync <SmtpException>(() => notification.SendBirthday(new[] { new EmailInfo("foo", "*****@*****.**"), new EmailInfo("bar", "*****@*****.**"), })); Assert.Equal(SmtpStatusCode.GeneralFailure, ex.StatusCode); ReceivedMail.FromAll(smtpServer) .Should() .BeEquivalentTo(new ReceivedMail(smtpConfiguration.Sender, "*****@*****.**", "Happy birthday!", "Happy birthday, dear foo!")); }
public async Task SendManyGreetingsWhenManyBirthdays() { File(fileConfiguration.FilePath, Header(), Employee("Matteo", "1982/09/11", "*****@*****.**"), Employee("John", "1982/10/08", "*****@*****.**"), Employee("Mary", "1975/09/11", "*****@*****.**")); await app.Run(Date("11/09/2020")); ReceivedMail.FromAll(smtpServer) .Should() .BeEquivalentTo( new ReceivedMail(smtpConfiguration.Sender, "*****@*****.**", "Happy birthday!", "Happy birthday, dear Mary!"), new ReceivedMail(smtpConfiguration.Sender, "*****@*****.**", "Happy birthday!", "Happy birthday, dear Matteo!") ); }
/// <summary> /// 获取邮件正文和附件(直接修改) /// </summary> /// <param name="mail">邮件原始信息</param> /// <param name="receivedMail">邮件</param> public static void GetMailText(string mail, ReceivedMail receivedMail) { string contentType = GetAttributeValue(mail, "Content-Type: ", ";"); //主体内容类型 if (contentType.Length == 0) { contentType = GetAttributeValue(mail, "Content-Type: ", "\n"); } int startIndex = -1; //主体内容开始处 int endIndex = -1; //主体内容结束处 string returnText = ""; string transfer = ""; string boundary = ""; //目标编码格式 string encodingName = GetAttributeValue(mail, "charset=\"", "\"").Replace("\"", ""); if (encodingName == "") { encodingName = GetAttributeValue(mail, "charset=", "\n").Replace("\n", ""); } Encoding encoding = Encoding.Default; if (encodingName != "") { encoding = Encoding.GetEncoding(encodingName); } switch (contentType) { case "text/html;": //传输格式 transfer = GetAttributeValue(mail, "\nContent-Transfer-Encoding: ", "\n").Trim(); startIndex = mail.IndexOf("\n\n"); if (startIndex != -1) { returnText = mail.Substring(startIndex, mail.Length - startIndex); } switch (transfer) { case "8bit": break; case "quoted-printable": returnText = DecodeQuotedPrintable(returnText, encoding); break; case "base64": returnText = DecodBase64(returnText, encoding); break; } //去掉最后的.和开头的\n\n receivedMail.Html = returnText.Substring(2, returnText.Length - 3); break; case "text/plain;": transfer = GetAttributeValue(mail, "Content-Transfer-Encoding: ", "\n").Trim(); startIndex = mail.IndexOf("\n\n"); if (startIndex != -1) { returnText = mail.Substring(startIndex, mail.Length - startIndex); } switch (transfer) { case "8bit": break; case "quoted-printable": returnText = DecodeQuotedPrintable(returnText, encoding); break; case "base64": returnText = DecodBase64(returnText, encoding); break; } //mailTable.Rows.Add(new object[] { "text/plain", returnText }); receivedMail.Body = returnText; receivedMail.IsText = true; break; case "multipart/alternative;": boundary = GetAttributeValue(mail, "boundary=\"", "\"").Replace("\"", ""); startIndex = mail.IndexOf("--" + boundary + "\n"); if (startIndex == -1) { return; } while (true) { endIndex = mail.IndexOf("--" + boundary, startIndex + boundary.Length); if (endIndex == -1) { break; } GetMailText(mail.Substring(startIndex, endIndex - startIndex), receivedMail); startIndex = endIndex; } break; case "multipart/related;": boundary = GetAttributeValue(mail, "boundary=\"", "\"").Replace("\"", ""); startIndex = mail.IndexOf("--" + boundary + "\n"); if (startIndex == -1) { return; } while (true) { endIndex = mail.IndexOf("--" + boundary, startIndex + boundary.Length); if (endIndex == -1) { break; } GetMailText(mail.Substring(startIndex, endIndex - startIndex), receivedMail); startIndex = endIndex; } break; case "multipart/mixed;": boundary = GetAttributeValue(mail, "boundary=\"", "\"").Replace("\"", ""); startIndex = mail.IndexOf("--" + boundary + "\n"); if (startIndex == -1) { return; } while (true) { endIndex = mail.IndexOf("--" + boundary, startIndex + boundary.Length); if (endIndex == -1) { break; } GetMailText(mail.Substring(startIndex, endIndex - startIndex), receivedMail); startIndex = endIndex; } break; default: //附件 if (contentType.IndexOf("application/") == 0) { startIndex = mail.IndexOf("\n\n"); if (startIndex != -1) { returnText = mail.Substring(startIndex, mail.Length - startIndex); } transfer = GetAttributeValue(mail, "\nContent-Transfer-Encoding: ", "\n").Trim(); string name = GetAttributeValue(mail, "filename=\"", "\"").Replace("\"", ""); name = GetReadText(name); byte[] fileBytes = new byte[0]; switch (transfer) { case "base64": fileBytes = Convert.FromBase64String(returnText); EnclosureUtil.Save(fileBytes, name, receivedMail.Id); break; } receivedMail.Enclosures.Add(name); } else if (contentType == "") { //没有类型 //传输格式 startIndex = mail.IndexOf("\n\n"); if (startIndex != -1) { returnText = mail.Substring(startIndex, mail.Length - startIndex); } receivedMail.Body = returnText.Substring(2, returnText.Length - 4); receivedMail.IsText = true; } break; } }
//显示邮件内容 private void ShowMailText(ReceivedMail receivedMail) { if (receivedMail == null) { label_sender.Text = "发件人:"; label_receiver.Text = "收件人:"; label_subject.Text = "主 题:"; label_date_detail.Text = "时间:"; label_size.Text = "大 小: KB"; label_enclosure.Text = "附 件: "; richTextBox_content.Text = ""; webBrowser_html.DocumentText = ""; listView_enclosure.Items.Clear(); webBrowser_html.Visible = false; richTextBox_content.Visible = true; groupBox_infos.Size = new Size(groupBox_infos.Size.Width, 156); richTextBox_content.Location = new Point(richTextBox_content.Location.X, 330 - 160); richTextBox_content.Size = new Size(richTextBox_content.Size.Width, 355 + 160); webBrowser_html.Location = new Point(webBrowser_html.Location.X, 355 - 160); webBrowser_html.Size = new Size(groupBox_infos.Size.Width, 355 + 160); return; } label_sender.Text = "发件人:" + receivedMail.FromName + " <" + receivedMail.FromAddress + ">"; label_receiver.Text = "收件人:" + receivedMail.ToName + " <" + receivedMail.ToAdderss + ">"; label_subject.Text = "主 题:" + receivedMail.Subject; label_date_detail.Text = "时间:" + receivedMail.SendDateTime.ToString(); label_size.Text = "大 小:" + receivedMail.Size.ToString() + " KB"; label_enclosure.Text = "附 件:(已下载于" + EnclosureUtil.GetDirOrPath() + ")"; richTextBox_content.Text = receivedMail.Body; int len = receivedMail.Enclosures.Count; listView_enclosure.Items.Clear(); webBrowser_html.Visible = false; richTextBox_content.Visible = true; if (len == 0) { groupBox_infos.Size = new Size(groupBox_infos.Size.Width, 156); richTextBox_content.Location = new Point(richTextBox_content.Location.X, 330 - 160); richTextBox_content.Size = new Size(richTextBox_content.Size.Width, 355 + 160); webBrowser_html.Location = new Point(webBrowser_html.Location.X, 355 - 160); webBrowser_html.Size = new Size(groupBox_infos.Size.Width, 355 + 160); } else { groupBox_infos.Size = new Size(groupBox_infos.Size.Width, 304); richTextBox_content.Location = new Point(richTextBox_content.Location.X, 330); richTextBox_content.Size = new Size(richTextBox_content.Size.Width, 355); webBrowser_html.Location = new Point(webBrowser_html.Location.X, 330); webBrowser_html.Size = new Size(webBrowser_html.Size.Width, 355); for (int i = 0; i < len; i++) { ListViewItem item = new ListViewItem(); string filename = receivedMail.Enclosures[i]; item.ImageIndex = EnclosureUtil.GetEnclosuerIconIndex(filename); item.Text = filename; listView_enclosure.Items.Add(item); } } ReceivedMail mail = GetSelectedMail(); if (mail != null && !mail.IsText) { webBrowser_html.Visible = true; richTextBox_content.Visible = false; webBrowser_html.DocumentText = mail.Html; } }