Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on attachment reading with mailkit implementation. #775

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 51 additions & 48 deletions dotnet/src/dotnetframework/GxMail/MailMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,68 +624,71 @@ public DateTime GetDateReceived()
*/
internal static DateTime GetMessageDate(string stringDate)
{
DateTime messageTime;
DateTime messageTime = DateTime.Now;
try
{
bool oldDateSpec = (stringDate.IndexOf("/") != -1);
bool hasStrDay = (stringDate.IndexOf(",") > 0); //StrDay is not empty
string dateStr = RemoveQuotes(stringDate.Replace("/", " ").Replace(",", "").Trim());
string[] dateParts = dateStr.Split(" ".ToCharArray());
if (!string.IsNullOrEmpty(stringDate))
{
bool oldDateSpec = (stringDate.IndexOf("/") != -1);
bool hasStrDay = (stringDate.IndexOf(",") > 0); //StrDay is not empty
string dateStr = RemoveQuotes(stringDate.Replace("/", " ").Replace(",", "").Trim());
string[] dateParts = dateStr.Split(" ".ToCharArray());

int i = hasStrDay?0:-1;
if (!IsNumber(dateParts[i+1]))
i = i + 1;
int i = hasStrDay ? 0 : -1;
if (!IsNumber(dateParts[i + 1]))
i = i + 1;

if (oldDateSpec)
{
string[] tmpDateParts = new string[dateParts.Length];
for(int j=0; j<dateParts.Length; j++)
if (oldDateSpec)
{
if (j == (i+1))
tmpDateParts[j] = dateParts[i+2];
else if (j == (i+2))
tmpDateParts[j] = dateParts[i+1];
else
tmpDateParts[j] = dateParts[j];
string[] tmpDateParts = new string[dateParts.Length];
for (int j = 0; j < dateParts.Length; j++)
{
if (j == (i + 1))
tmpDateParts[j] = dateParts[i + 2];
else if (j == (i + 2))
tmpDateParts[j] = dateParts[i + 1];
else
tmpDateParts[j] = dateParts[j];
}
dateParts = tmpDateParts;
}
dateParts = tmpDateParts;
}

//parse date
int day = int.Parse(dateParts[i+1]);
int month = oldDateSpec?int.Parse(dateParts[i+2]):GetMonthIndex(dateParts[i+2]);
int year = int.Parse(dateParts[i+3]);
//parse date
int day = int.Parse(dateParts[i + 1]);
int month = oldDateSpec ? int.Parse(dateParts[i + 2]) : GetMonthIndex(dateParts[i + 2]);
int year = int.Parse(dateParts[i + 3]);

//parse time
string[] tmpTime = dateParts[i+4].Split(":".ToCharArray());
int hr = int.Parse(tmpTime[0]);
int mm = 0, ss = 0;
//parse time
string[] tmpTime = dateParts[i + 4].Split(":".ToCharArray());
int hr = int.Parse(tmpTime[0]);
int mm = 0, ss = 0;

if (tmpTime.Length > 1)
mm = int.Parse(tmpTime[1]);
if (tmpTime.Length > 1)
mm = int.Parse(tmpTime[1]);

if (tmpTime.Length > 2)
ss = int.Parse(tmpTime[2]);
if (tmpTime.Length > 2)
ss = int.Parse(tmpTime[2]);

string timeZoneOffset = string.Empty;
if (dateParts.Length > i + 5)
{
timeZoneOffset = dateParts[i + 5];
}
string timeZoneOffset = string.Empty;
if (dateParts.Length > i + 5)
{
timeZoneOffset = dateParts[i + 5];
}

messageTime = DateTimeUtil.DateTimeWithTimeZoneToUTC(year, month, day, hr, mm, ss, timeZoneOffset);
if (GeneXus.Application.GxContext.Current != null)
{
messageTime = DateTimeUtil.FromTimeZone(messageTime, "Etc/UTC", GeneXus.Application.GxContext.Current);
}
else
{
messageTime = messageTime.ToLocalTime();
}
messageTime = DateTimeUtil.DateTimeWithTimeZoneToUTC(year, month, day, hr, mm, ss, timeZoneOffset);
if (GeneXus.Application.GxContext.Current != null)
{
messageTime = DateTimeUtil.FromTimeZone(messageTime, "Etc/UTC", GeneXus.Application.GxContext.Current);
}
else
{
messageTime = messageTime.ToLocalTime();
}
}
}
catch
catch(Exception ex)
{
messageTime = DateTime.Now;
GXLogging.Warn(log, $"Date {stringDate} can't be parsed", ex);
}
return messageTime;
}
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/dotnetframework/GxMail/Pop3MailKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private string GetHeaderFromMimeMessage(MimeMessage msg, string headerValue)

private void ProcessMailAttachments(GXMailMessage gxmessage, IEnumerable<MimeEntity> attachs)
{
if (attachs == null || attachs.GetEnumerator().MoveNext()) // Si MoveNext es false significa que el attach viene vacio
if (attachs == null)
return;

if (DownloadAttachments)
Expand Down Expand Up @@ -229,7 +229,7 @@ private void ProcessMailAttachments(GXMailMessage gxmessage, IEnumerable<MimeEnt

private void SaveAttachedFile(MimeEntity attach, string attachName)
{
using (var stream = File.Create(AttachDir + attachName))
using (var stream = File.Create(Path.Combine(AttachDir, attachName)))
{
if (attach is MessagePart)
{
Expand Down