Проблема: Я сделал простое почтовое приложение с использованием RichTextBox, которое позволяет пользователю отправлять сообщения. Пользователи могут изменять тип шрифта, размер, стиль, выравнивание и цвет. После отправки и получения электронного письма цвета не отображаются.
Известные переменные: Настройка MailMessage.IsBodyHtml = true, затем с помощью (здесь сообщение) будет отображать цвет при получении письма.
Вопрос: Мое решение показано ниже, какие существуют альтернативы / решения при отправке цветовых данных по электронной почте?
Клиент и результат: Мой почтовый клиент находится слева. Справа показан мой результат ДО использования раствора и ПОСЛЕ использования решения, показанного ниже.
См. Изображение клиента и результата
Как мне отправить электронное письмо:
public bool SendEmail(string _strHostServer, int _nPort,
string _strSendToAddress, string _strFromAddress,
string _strUsername, string _strPassword,
string _strSubject, string _strBody)
{
// Local variable.
bool bSuccess = true;
// Create a message and set up the recipients.
MailMessage message = new MailMessage(_strFromAddress, _strSendToAddress, _strSubject, "<html><head></head><body>" + _strBody + "</body></html>");
message.IsBodyHtml = true;
// Setup client and add credentials if the SMTP server requires them.
SmtpClient client = new SmtpClient(_strHostServer, _nPort);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(_strUsername, _strPassword);
// Attempt to send the e-mail.
try
{
client.Send(message);
}
// Catch all exceptions.
catch
{
bSuccess = false;
}
return bSuccess;
}
Мое решение "Brute Force":
/// <summary>
/// Convert the .NET RichTextBox message into an HTML format.
/// </summary>
/// <param name="_rtb">The .NET RichTextBox to parse.</param>
private string ConvertRTBtoHTML(RichTextBox _rtb)
{
// Local variables.
StringBuilder builder = new StringBuilder(_rtb.Text);
int nCurrentIndex = 0;
int nInsertIndex = 0;
int nMaxLength = _rtb.Text.Length;
Color colorCurrent = Color.Black;
// Run through the RichTextBox message, until we reach the end.
while (nCurrentIndex < nMaxLength)
{
// Highlight the character at the current index and store the fore color.
_rtb.Select(nCurrentIndex, 1);
colorCurrent = _rtb.SelectionColor;
// If the fore color is black, increment indexes and continue looping...
if (colorCurrent == Color.Black)
{
++nCurrentIndex;
++nInsertIndex;
}
// ...otherwise, insert HTML tags.
else
{
// Insert <font color = #(fore color)> at the insert index, then increment after the '>' location.
builder.Insert(nInsertIndex, "<font color = #" + Helper.ColorToHex(colorCurrent, Helper.HexPattern.RGB) + ">");
nInsertIndex += 23;
// Run through the next characters.
while (true)
{
// Highlight the character at the next current index.
_rtb.Select(++nCurrentIndex, 1);
// If the fore color doesn't match or we're at the end, break.
if (colorCurrent != _rtb.SelectionColor || nCurrentIndex >= nMaxLength)
break;
// Otherwise, increment insert index.
++nInsertIndex;
}
// Insert </font> at the insert index, then increment after the '>' location.
builder.Insert(nInsertIndex, "</font>");
nInsertIndex += 7;
}
}
// Success!
return builder.ToString();
}
Заранее спасибо!