/// <summary> /// 将邮件保存到草稿箱 返回邮件的唯一标识 /// 调用前先调用配置方法CfgIMAP(),调用制做邮件方法 /// </summary> public int SaveDrafts(int uniqueId = -1) { try { using (ImapClient client = ConnectIMAP()) { // 打开草稿箱,添加邮件 IMailFolder folder = client.GetFolder(SpecialFolder.Drafts); folder.Open(FolderAccess.ReadWrite); // 如果保存的是已经有的草稿邮件,则删除它再保存新的草稿.(没找到保存已有草稿的办法) if (uniqueId > -1) { List <UniqueId> uidls = new List <UniqueId>(); uidls.Add(new UniqueId((uint)uniqueId)); folder.SetFlags(uidls, MessageFlags.Seen | MessageFlags.Deleted, true); folder.Expunge(uidls); } UniqueId?uid = folder.Append(this.message, MessageFlags.Seen | MessageFlags.Draft); // folder.Close(); client.Disconnect(true); return(uid.HasValue ? (int)uid.Value.Id : -1); } } catch (Exception e) { ErrMsg = $"邮件保存草稿时异常:{e.ToString()} [{e.Message}]"; return(-1); } }
private void ToSent(string login, string password, MailMessage message) { ImapClient imap = new ImapClient(); imap.Connect("imap.yandex.ru", 993, true); // imap server address imap.Authenticate(login, password); FolderNamespace folderNamespaceSent = new FolderNamespace('/', "Отправленные"); // getter sent folder IMailFolder folderSent = imap.GetFolder(folderNamespaceSent); folderSent.Open(FolderAccess.ReadOnly); MimeMessage mimeMessage = (MimeMessage)message; folderSent.Append(mimeMessage); // add to sent folder }
private void CopyMessage(IMessageSummary msg, IMailFolder folder, IMailFolder dest) { if (MessageExists(msg, folder, dest)) { return; } var m = folder.GetMessage(msg.UniqueId); //string rd = msg.Headers[HeaderId.Received]; try { dest.Append(m, msg.Flags == null ? MessageFlags.None : msg.Flags.Value, msg.InternalDate ?? (DateTimeOffset.Now)); } catch (Exception ex) { /*if (!ex.Message.Contains("Message too large.")) { * throw; * }*/ // failed... string failedFolder = "failed\\" + folder.FullName.Replace(folder.DirectorySeparator, '-'); string failed = failedFolder + "\\" + DateTime.UtcNow.Ticks + ".msg"; string reason = failed + ".reason.txt"; string dir = System.IO.Path.GetDirectoryName(failed); if (!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } using (var fs = System.IO.File.OpenWrite(failed)) { m.WriteTo(fs); } System.IO.File.WriteAllText(reason, ex.ToString()); } StoreLocal(msg, folder); }
private IMailFolder GetOrCreateLockFolder(IMailFolder parentFolderOfLockFolder, string resourceName, CancellationToken cancellationToken = default) { var lockFolderName = parentFolderOfLockFolder.MakeSafeFolderName(resourceName); IMailFolder lockFolder = null; try { lockFolder = parentFolderOfLockFolder.GetSubfolder(lockFolderName, cancellationToken); } catch { logger.Information("Could not get lock folder '{LockFolderName}' - maybe it does not yet exist or there was an error", lockFolderName); } if (lockFolder == null) { try { logger.Information("Creating new lock folder with name '{LockFolderName}'", lockFolderName); // note: on some mail servers (Greenmail) this will throw if the folder already exists, other servers don't throw and return any existing foldre with this name (Dovecot) lockFolder = parentFolderOfLockFolder.Create(lockFolderName, true); } catch { logger.Information("Error while creating lock folder '{LockFolderName}' - maybe it already exists or ther was an error", lockFolderName); } } if (lockFolder == null) { throw new TeasmCompanionException($"Could not get or create lock folder '{lockFolderName}'"); } lockFolder.Open(FolderAccess.ReadOnly); var existingLockMessages = lockFolder.Search(SearchQuery.SubjectContains(LockMessageSubject), cancellationToken); if (existingLockMessages.Count == 0) { // note: this might create duplicates if run by multiple clients; we'll remove the duplicates later lockFolder.Append(CreateLockMimeMessage()); Thread.Sleep(1000); // give other parallel clients the chance to produce their duplicates if any; we then always choose the first one (selected by UniqueId) } return(lockFolder); }
public void Send(InternetMailMessage message, InternetMailTextProcessing processText) { var messageToSend = message.CreateNativeMessage(processText); _currentFolder.Append(messageToSend); }