Skip to content

Commit cb281d1

Browse files
committed
When adding a pdf file as an attachment to an email, it is sent with content type application/octet-stream instead of application/pdf
Issue: 205900
1 parent 2970ed8 commit cb281d1

File tree

3 files changed

+86
-61
lines changed

3 files changed

+86
-61
lines changed

common/src/main/java/com/genexus/CommonUtil.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,4 +3434,68 @@ public static String getClassName(String pgmName) {
34343434

34353435
return classPackage + pgmName.replace('\\', '.').trim();
34363436
}
3437+
3438+
public static boolean isKnownContentType(String type)
3439+
{
3440+
if (type != null)
3441+
{
3442+
for (int i = 0; i < contentTypes.length; i++)
3443+
{
3444+
if (contentTypes[i].length >= 2)
3445+
{
3446+
if (type.equalsIgnoreCase(contentTypes[i][1]))
3447+
return true;
3448+
}
3449+
}
3450+
}
3451+
return false;
3452+
}
3453+
3454+
public static String getContentFromExt( String extension)
3455+
{
3456+
if (extension != null)
3457+
{
3458+
extension = extension.toLowerCase();
3459+
for (int i = 0; i < contentTypes.length; i++) {
3460+
if (contentTypes[i][0].equals(extension.trim()))
3461+
return contentTypes[i][1];
3462+
}
3463+
}
3464+
return null;
3465+
}
3466+
3467+
private static final String contentTypes[][] = {
3468+
{"txt" , "text/plain"},
3469+
{"rtx" , "text/richtext"},
3470+
{"htm" , "text/html"},
3471+
{"html" , "text/html"},
3472+
{"xml" , "text/xml"},
3473+
{"aif" , "audio/x-aiff"},
3474+
{"au" , "audio/basic"},
3475+
{"wav" , "audio/wav"},
3476+
{"bmp" , "image/bmp"},
3477+
{"gif" , "image/gif"},
3478+
{"jpe" , "image/jpeg"},
3479+
{"jpeg" , "image/jpeg"},
3480+
{"jpg" , "image/jpeg"},
3481+
{"jfif" , "image/pjpeg"},
3482+
{"tif" , "image/tiff"},
3483+
{"tiff" , "image/tiff"},
3484+
{"png" , "image/x-png"},
3485+
{"3gp" , "video/3gpp"},
3486+
{"3g2" , "video/3gpp2"},
3487+
{"mpg" , "video/mpeg"},
3488+
{"mpeg" , "video/mpeg"},
3489+
{"mov" , "video/quicktime"},
3490+
{"qt" , "video/quicktime"},
3491+
{"avi" , "video/x-msvideo"},
3492+
{"exe" , "application/octet-stream"},
3493+
{"dll" , "application/x-msdownload"},
3494+
{"ps" , "application/postscript"},
3495+
{"pdf" , "application/pdf"},
3496+
{"svg" , "image/svg+xml"},
3497+
{"tgz" , "application/x-compressed"},
3498+
{"zip" , "application/x-zip-compressed"},
3499+
{"gz" , "application/x-gzip"}
3500+
};
34373501
}

gxmail/src/main/java/com/genexus/internet/SMTPSessionJavaMail.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,31 @@ private void addAttachment(Multipart multipart, String fileNamePath, String atta
217217
{
218218
fileNamePath = attachDir + fileNamePath;
219219
}
220-
BodyPart messageBodyPart = new MimeBodyPart();
221-
DataSource source = new FileDataSource(fileNamePath);
222-
messageBodyPart.setDataHandler(new DataHandler(source));
220+
223221
if (filename.lastIndexOf(File.separator) != -1)
224222
{
225223
filename = filename.substring(filename.lastIndexOf(File.separator) + 1);
226224
}
225+
226+
int lastDot = filename.lastIndexOf('.');
227+
String extension = (lastDot == -1) ? "" : filename.substring(lastDot + 1).toLowerCase();
228+
229+
String mt = CommonUtil.getContentFromExt(extension);
230+
final String mimeType = (mt == null || mt.isEmpty()) ? "application/octet-stream" : mt;
231+
232+
DataSource source = new FileDataSource(fileNamePath) {
233+
@Override
234+
public String getContentType() {
235+
return mimeType;
236+
}
237+
};
238+
239+
BodyPart messageBodyPart = new MimeBodyPart();
240+
messageBodyPart.setDataHandler(new DataHandler(source));
227241
messageBodyPart.setFileName(filename);
242+
228243
multipart.addBodyPart(messageBodyPart);
229-
}
244+
}
230245

231246
public void logout(GXSMTPSession sessionInfo)
232247
{

java/src/main/java/com/genexus/internet/HttpContext.java

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,31 +1719,12 @@ public boolean checkContentType(String contentKey, String contentType, String fu
17191719

17201720
public static boolean isKnownContentType(String type)
17211721
{
1722-
if (type != null)
1723-
{
1724-
for (int i = 0; i < contentTypes.length; i++)
1725-
{
1726-
if (contentTypes[i].length >= 2)
1727-
{
1728-
if (type.equalsIgnoreCase(contentTypes[i][1]))
1729-
return true;
1730-
}
1731-
}
1732-
}
1733-
return false;
1722+
return CommonUtil.isKnownContentType(type);
17341723
}
17351724

17361725
public static String getContentFromExt( String extension)
17371726
{
1738-
if (extension != null)
1739-
{
1740-
extension = extension.toLowerCase();
1741-
for (int i = 0; i < contentTypes.length; i++) {
1742-
if (contentTypes[i][0].equals(extension.trim()))
1743-
return contentTypes[i][1];
1744-
}
1745-
}
1746-
return null;
1727+
return CommonUtil.getContentFromExt(extension);
17471728
}
17481729

17491730
int GX_NULL_TIMEZONEOFFSET = 9999;
@@ -1755,42 +1736,7 @@ public void setRestService()
17551736

17561737
public boolean isRestService()
17571738
{ return restService; }
1758-
1759-
private static final String contentTypes[][] = {
1760-
{"txt" , "text/plain"},
1761-
{"rtx" , "text/richtext"},
1762-
{"htm" , "text/html"},
1763-
{"html" , "text/html"},
1764-
{"xml" , "text/xml"},
1765-
{"aif" , "audio/x-aiff"},
1766-
{"au" , "audio/basic"},
1767-
{"wav" , "audio/wav"},
1768-
{"bmp" , "image/bmp"},
1769-
{"gif" , "image/gif"},
1770-
{"jpe" , "image/jpeg"},
1771-
{"jpeg" , "image/jpeg"},
1772-
{"jpg" , "image/jpeg"},
1773-
{"jfif" , "image/pjpeg"},
1774-
{"tif" , "image/tiff"},
1775-
{"tiff" , "image/tiff"},
1776-
{"png" , "image/x-png"},
1777-
{"3gp" , "video/3gpp"},
1778-
{"3g2" , "video/3gpp2"},
1779-
{"mpg" , "video/mpeg"},
1780-
{"mpeg" , "video/mpeg"},
1781-
{"mov" , "video/quicktime"},
1782-
{"qt" , "video/quicktime"},
1783-
{"avi" , "video/x-msvideo"},
1784-
{"exe" , "application/octet-stream"},
1785-
{"dll" , "application/x-msdownload"},
1786-
{"ps" , "application/postscript"},
1787-
{"pdf" , "application/pdf"},
1788-
{"svg" , "image/svg+xml"},
1789-
{"tgz" , "application/x-compressed"},
1790-
{"zip" , "application/x-zip-compressed"},
1791-
{"gz" , "application/x-gzip"}
1792-
};
1793-
1739+
17941740
public boolean willRedirect()
17951741
{
17961742
return wjLoc != null && wjLoc.trim().length() != 0;

0 commit comments

Comments
 (0)