从Exchange邮件服务器读取邮件 [英] Read Mails From Exchange mail server
本文介绍了从Exchange邮件服务器读取邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过TLS身份验证从我的邮件服务器收到邮件,如果你能帮助我,我会很高兴。
Thx
在这里我附上我所拥有的东西
I need to mails from my mail server with TLS authentication, If you can help me i would be gladful.
Thx
here i have attach what i have
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Stardeveloper.Pop3" %>
<script runat="server">
public const string Host = "mailserver";
public const int Port = 110;
public const string Email = "email address";
public const string Password = "password#";
protected static Regex CharsetRegex =
new Regex("charset=\"?(?<charset>[^\\s\"]+)\"?",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
protected static Regex QuotedPrintableRegex =
new Regex("=(?<hexchars>[0-9a-fA-F]{2,2})",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
protected static Regex UrlRegex =
new Regex("(?<url>https?://[^\\s\"]+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
protected static Regex FilenameRegex =
new Regex("filename=\"?(?<filename>[^\\s\"]+)\"?",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
protected static Regex NameRegex =
new Regex("name=\"?(?<filename>[^\\s\"]+)\"?",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
protected void Page_Load(object source, EventArgs e)
{
/* Setting email id */
int emailId = 5;
//Session["emailId"] = emailId;
/*if (Request.QueryString["emailId"] == null)
{
Response.Redirect("Pop3Client.aspx");
Response.Flush();
Response.End();
}
else
emailId = Convert.ToInt32(
Request.QueryString["emailId"]);
*/
/* Creating Pop3Client object and accessing email data */
Email email = null;
List<MessagePart> msgParts = null;
using (Pop3Client client = new Pop3Client(Host, Port,
Email, Password, false,true))
{
client.Connect();
// Fetching email headers
email = client.FetchEmail(emailId);
// Fetching email body
msgParts = client.FetchMessageParts(emailId);
}
if (email == null || msgParts == null)
{
//Response.Redirect("Pop3Client.aspx");
Response.Flush();
Response.End();
}
/* Selecting a message part to display to the user */
MessagePart preferredMsgPart = FindMessagePart(msgParts, "text/html");
if (preferredMsgPart == null)
preferredMsgPart = FindMessagePart(msgParts, "text/plain");
else if (preferredMsgPart == null && msgParts.Count > 0)
preferredMsgPart = msgParts[0];
string contentType, charset, contentTransferEncoding, body = null;
if (preferredMsgPart != null)
{
contentType = preferredMsgPart.Headers["Content-Type"];
charset = "us-ascii"; // default charset
contentTransferEncoding =
preferredMsgPart.Headers["Content-Transfer-Encoding"];
Match m = CharsetRegex.Match(contentType);
if (m.Success)
charset = m.Groups["charset"].Value;
HeadersLiteral.Text = contentType != null ? "Content-Type: " +
contentType + "<br />" : string.Empty;
HeadersLiteral.Text += contentTransferEncoding != null ?
"Content-Transfer-Encoding: " +
contentTransferEncoding : string.Empty;
/* Decoding base64 and quoted-printable encoded messages */
if (contentTransferEncoding != null)
{
if (contentTransferEncoding.ToLower() == "base64")
body = DecodeBase64String(charset,
preferredMsgPart.MessageText);
else if (contentTransferEncoding.ToLower() ==
"quoted-printable")
body = DecodeQuotedPrintableString(
preferredMsgPart.MessageText);
else
body = preferredMsgPart.MessageText;
}
else
body = preferredMsgPart.MessageText;
}
/* Displaying information to the user */
EmailIdLiteral.Text = Convert.ToString(emailId);
DateLiteral.Text = email.UtcDateTime.ToString(); ;
FromLiteral.Text = email.From;
SubjectLiteral.Text = email.Subject;
BodyLiteral.Text = preferredMsgPart != null ?
(preferredMsgPart.Headers["Content-Type"]
.IndexOf("text/plain") != -1 ?
"<pre>" + FormatUrls(body) + "</pre>" : body) : null;
ListAttachments(msgParts);
}
protected Decoder GetDecoder(string charset)
{
Decoder decoder;
switch (charset.ToLower())
{
case "utf-7":
decoder = Encoding.UTF7.GetDecoder();
break;
case "utf-8":
decoder = Encoding.UTF8.GetDecoder();
break;
case "us-ascii":
decoder = Encoding.ASCII.GetDecoder();
break;
case "iso-8859-1":
decoder = Encoding.ASCII.GetDecoder();
break;
default:
decoder = Encoding.ASCII.GetDecoder();
break;
}
return decoder;
}
protected string DecodeBase64String(string charset, string encodedString)
{
Decoder decoder = GetDecoder(charset);
byte[] buffer = Convert.FromBase64String(encodedString);
char[] chararr = new char[decoder.GetCharCount(buffer,
0, buffer.Length)];
decoder.GetChars(buffer, 0, buffer.Length, chararr, 0);
return new string(chararr);
}
protected string DecodeQuotedPrintableString(string encodedString)
{
StringBuilder b = new StringBuilder();
int startIndx = 0;
MatchCollection matches = QuotedPrintableRegex.Matches(encodedString);
for (int i = 0; i < matches.Count; i++)
{
Match m = matches[i];
string hexchars = m.Groups["hexchars"].Value;
int charcode = Convert.ToInt32(hexchars, 16);
char c = (char)charcode;
if (m.Index > 0)
b.Append(encodedString.Substring(startIndx,
(m.Index - startIndx)));
b.Append(c);
startIndx = m.Index + 3;
}
if (startIndx < encodedString.Length)
b.Append(encodedString.Substring(startIndx));
return Regex.Replace(b.ToString(), "=\r\n", "");
}
protected void ListAttachments(List<MessagePart> msgParts)
{
bool attachmentsFound = false;
StringBuilder b = new StringBuilder();
b.Append("<ol>");
foreach (MessagePart p in msgParts)
{
string contentType = p.Headers["Content-Type"];
string contentDisposition = p.Headers["Content-Disposition"];
Match m;
if (contentDisposition != null)
{
m = FilenameRegex.Match(contentDisposition);
if (m.Success)
{
attachmentsFound = true;
b.Append("<li>").
Append(m.Groups["filename"].
Value).Append("</li>");
}
}
else if (contentType != null)
{
m = NameRegex.Match(contentType);
if (m.Success)
{
attachmentsFound = true;
b.Append("<li>").
Append(m.Groups["filename"].
Value).Append("</li>");
}
}
}
b.Append("</ol>");
if (attachmentsFound)
AttachmentsLiteral.Text = b.ToString();
else
AttachementsRow.Visible = false;
}
protected MessagePart FindMessagePart(List<MessagePart> msgParts,
string contentType)
{
foreach (MessagePart p in msgParts)
if (p.ContentType != null &&
p.ContentType.IndexOf(contentType) != -1)
return p;
return null;
}
protected string FormatUrls(string plainText)
{
string replacementLink = "<a href=\"${url}\">${url}</a>";
return UrlRegex.Replace(plainText, replacementLink);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.emails-table { width: 600px; border: solid 1px #444444; }
.emails-table-header { font-family: "Trebuchet MS"; font-size: 9pt;
background-color: #0099B9; font-weight: bold; color: white;
text-align: center; border: solid 1px #444444; }
.emails-table-header-cell { font-family: "Georgia"; font-size: 9pt;
font-weight: bold; border: solid 1px #666666; padding: 6px; }
.emails-table-cell { font-family: "Georgia"; font-size: 9pt;
border: solid 1px #666666; padding: 6px; }
.emails-table-footer { border: solid 1px #666666; padding: 3px;
width: 50%; }
.email-datetime { float: right; color: #666666; }
a { font-family: "Lucida Sans Unicode", "Trebuchet MS"; font-size: 9pt;
color: #005B7F; }
a:hover { color:red; }
pre { font-family: "Georgia"; font-size: 9pt; }
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Literal ID="DebugLiteral" runat="server" />
<table class="emails-table">
<tr>
<td class="emails-table-header" colspan="2">
Email #<asp:Literal ID="EmailIdLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-header-cell">Date & Time</td>
<td class="emails-table-cell">
<asp:Literal ID="DateLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-header-cell">From</td>
<td class="emails-table-cell">
<asp:Literal ID="FromLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-header-cell">Subject</td>
<td class="emails-table-cell">
<asp:Literal ID="SubjectLiteral" runat="server" /></td>
</tr>
<tr id="AttachementsRow" runat="server">
<td class="emails-table-header-cell">Attachments</td>
<td class="emails-table-cell">
<asp:Literal ID="AttachmentsLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-cell" colspan="2">
<asp:Literal ID="HeadersLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-cell" colspan="2">
<asp:Literal ID="BodyLiteral" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
推荐答案
{url}\\">
{url}\">
{url}</a>\" ;
return UrlRegex.Replace(plainText, replacementLink);
}
</script>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.or g/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head id=\"Head1\" runat=\"server\">
<title></title>
<style type=\"text/css\">
.emails-table { width: 600px; border: solid 1px #444444; }
.emails-table-header { font-family: \"Trebuchet MS\"; font-size: 9pt;
background-color: #0099B9; font-weight: bold; color: white;
text-align: center; border: solid 1px #444444; }
.emails-table-header-cell { font-family: \"Georgia\"; font-size: 9pt;
font-weight: bold; border: solid 1px #666666; padding: 6px; }
.emails-table-cell { font-family: \"Georgia\"; font-size: 9pt;
border: solid 1px #666666; padding: 6px; }
.emails-table-footer { border: solid 1px #666666; padding: 3px;
width: 50%; }
.email-datetime { float: right; color: #666666; }
a { font-family: \"Lucida Sans Unicode\", \"Trebuchet MS\"; font-size: 9pt;
color: #005B7F; }
a:hover { color:red; }
pre { font-family: \"Georgia\"; font-size: 9pt; }
</style>
</head>
<body>
<form id=\"form1\" runat=\"server\">
<asp:Literal ID=\"DebugLiteral\" runat=\"server\"/>
<table class=\"emails-table\">
<tr>
<td class=\"emails-table-header\" colspan=\"2\">
Email #<asp:Literal ID=\"EmailIdLiteral\" runat=\"server\" /></td>
</tr>
<tr>
<td class=\"emails-table-header-cell\">Date & Time</td>
<td class=\"emails-table-cell\">
<asp:Literal ID=\"DateLiteral\" runat=\"server\" /></td>
</tr>
<tr>
<td class=\"emails-table-header-cell\">From</td>
<td class=\"emails-table-cell\">
<asp:Literal ID=\"FromLiteral\" runat=\"server\" /></td>
</tr>
<tr>
<td class=\"emails-table-header-cell\">Subject</td>
<td class=\"emails-table-cell\">
<asp:Literal ID=\"SubjectLiteral\" runat=\"server\" /></td>
</tr>
<tr id=\"AttachementsRow\" run at=\"server\">
<td class=\"emails-table-header-cell\">Attachments</td>
<td class=\"emails-table-cell\">
<asp:Literal ID=\"AttachmentsLiteral\" runat=\"server\" /></td>
</tr>
<tr>
<td class=\"emails-table-cell\" colspan=\"2\">
<asp:Literal ID=\"HeadersLiteral\" runat=\"server\" /></td>
</tr>
<tr>
<td class=\"emails-table-cell\" colspan=\"2\">
<asp:Literal ID=\"BodyLiteral\" runat=\"server\"/></td>
</tr>
</table>
</form>
</body>
</html>
{url}</a>"; return UrlRegex.Replace(plainText, replacementLink); } </script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <style type="text/css"> .emails-table { width: 600px; border: solid 1px #444444; } .emails-table-header { font-family: "Trebuchet MS"; font-size: 9pt; background-color: #0099B9; font-weight: bold; color: white; text-align: center; border: solid 1px #444444; } .emails-table-header-cell { font-family: "Georgia"; font-size: 9pt; font-weight: bold; border: solid 1px #666666; padding: 6px; } .emails-table-cell { font-family: "Georgia"; font-size: 9pt; border: solid 1px #666666; padding: 6px; } .emails-table-footer { border: solid 1px #666666; padding: 3px; width: 50%; } .email-datetime { float: right; color: #666666; } a { font-family: "Lucida Sans Unicode", "Trebuchet MS"; font-size: 9pt; color: #005B7F; } a:hover { color:red; } pre { font-family: "Georgia"; font-size: 9pt; } </style> </head> <body> <form id="form1" runat="server"> <asp:Literal ID="DebugLiteral" runat="server" /> <table class="emails-table"> <tr> <td class="emails-table-header" colspan="2"> Email #<asp:Literal ID="EmailIdLiteral" runat="server" /></td> </tr> <tr> <td class="emails-table-header-cell">Date & Time</td> <td class="emails-table-cell"> <asp:Literal ID="DateLiteral" runat="server" /></td> </tr> <tr> <td class="emails-table-header-cell">From</td> <td class="emails-table-cell"> <asp:Literal ID="FromLiteral" runat="server" /></td> </tr> <tr> <td class="emails-table-header-cell">Subject</td> <td class="emails-table-cell"> <asp:Literal ID="SubjectLiteral" runat="server" /></td> </tr> <tr id="AttachementsRow" runat="server"> <td class="emails-table-header-cell">Attachments</td> <td class="emails-table-cell"> <asp:Literal ID="AttachmentsLiteral" runat="server" /></td> </tr> <tr> <td class="emails-table-cell" colspan="2"> <asp:Literal ID="HeadersLiteral" runat="server" /></td> </tr> <tr> <td class="emails-table-cell" colspan="2"> <asp:Literal ID="BodyLiteral" runat="server" /></td> </tr> </table> </form> </body> </html>
You would probably want to use the Exchange Web Services Managed API [^]
The API is documented here: Exchange Web Services (EWS)[^]
Here is a nice series on how to use EWS:
Exchange Web Services Example – Part 1 – Introduction and Set up[^]
Exchange Web Services Example – Part 2 – Creating Appointments[^]
OpenEdge GUI for .NET – Testing the Bridge[^]
Exchange Web Services Example – Part 3 – Exchange Impersonation[^]
Exchange Web Services Example – Part 4 – Subscriptions and Notifications[^]
Then we have, Exchange Web Services[^], which shows how to send an email in a few simple steps.
You can use the ProtocolConnection.EncryptionMethod[^] to specify the encryption.
Best regards
Espen Harlinn
You would probably want to use the Exchange Web Services Managed API [^]
The API is documented here: Exchange Web Services (EWS)[^]
Here is a nice series on how to use EWS:
Exchange Web Services Example – Part 1 – Introduction and Set up[^]
Exchange Web Services Example – Part 2 – Creating Appointments[^]
OpenEdge GUI for .NET – Testing the Bridge[^]
Exchange Web Services Example – Part 3 – Exchange Impersonation[^]
Exchange Web Services Example – Part 4 – Subscriptions and Notifications[^]
Then we have, Exchange Web Services[^], which shows how to send an email in a few simple steps.
You can use the ProtocolConnection.EncryptionMethod[^] to specify the encryption.
Best regards
Espen Harlinn
这篇关于从Exchange邮件服务器读取邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文