Skip to content

Commit 1ca0df7

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 e9ea58a commit 1ca0df7

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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,4 +3490,69 @@ public static String Sanitize(String input, HashMap<Character, Character> whiteL
34903490
}
34913491
return sanitizedInput.toString();
34923492
}
3493+
3494+
3495+
public static boolean isKnownContentType(String type)
3496+
{
3497+
if (type != null)
3498+
{
3499+
for (int i = 0; i < contentTypes.length; i++)
3500+
{
3501+
if (contentTypes[i].length >= 2)
3502+
{
3503+
if (type.equalsIgnoreCase(contentTypes[i][1]))
3504+
return true;
3505+
}
3506+
}
3507+
}
3508+
return false;
3509+
}
3510+
3511+
public static String getContentFromExt( String extension)
3512+
{
3513+
if (extension != null)
3514+
{
3515+
extension = extension.toLowerCase();
3516+
for (int i = 0; i < contentTypes.length; i++) {
3517+
if (contentTypes[i][0].equals(extension.trim()))
3518+
return contentTypes[i][1];
3519+
}
3520+
}
3521+
return null;
3522+
}
3523+
3524+
private static final String contentTypes[][] = {
3525+
{"txt" , "text/plain"},
3526+
{"rtx" , "text/richtext"},
3527+
{"htm" , "text/html"},
3528+
{"html" , "text/html"},
3529+
{"xml" , "text/xml"},
3530+
{"aif" , "audio/x-aiff"},
3531+
{"au" , "audio/basic"},
3532+
{"wav" , "audio/wav"},
3533+
{"bmp" , "image/bmp"},
3534+
{"gif" , "image/gif"},
3535+
{"jpe" , "image/jpeg"},
3536+
{"jpeg" , "image/jpeg"},
3537+
{"jpg" , "image/jpeg"},
3538+
{"jfif" , "image/pjpeg"},
3539+
{"tif" , "image/tiff"},
3540+
{"tiff" , "image/tiff"},
3541+
{"png" , "image/x-png"},
3542+
{"3gp" , "video/3gpp"},
3543+
{"3g2" , "video/3gpp2"},
3544+
{"mpg" , "video/mpeg"},
3545+
{"mpeg" , "video/mpeg"},
3546+
{"mov" , "video/quicktime"},
3547+
{"qt" , "video/quicktime"},
3548+
{"avi" , "video/x-msvideo"},
3549+
{"exe" , "application/octet-stream"},
3550+
{"dll" , "application/x-msdownload"},
3551+
{"ps" , "application/postscript"},
3552+
{"pdf" , "application/pdf"},
3553+
{"svg" , "image/svg+xml"},
3554+
{"tgz" , "application/x-compressed"},
3555+
{"zip" , "application/x-zip-compressed"},
3556+
{"gz" , "application/x-gzip"}
3557+
};
34933558
}

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: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -877,31 +877,12 @@ public boolean checkContentType(String contentKey, String contentType, String fu
877877

878878
public static boolean isKnownContentType(String type)
879879
{
880-
if (type != null)
881-
{
882-
for (int i = 0; i < contentTypes.length; i++)
883-
{
884-
if (contentTypes[i].length >= 2)
885-
{
886-
if (type.equalsIgnoreCase(contentTypes[i][1]))
887-
return true;
888-
}
889-
}
890-
}
891-
return false;
880+
return CommonUtil.isKnownContentType(type);
892881
}
893882

894883
public static String getContentFromExt( String extension)
895884
{
896-
if (extension != null)
897-
{
898-
extension = extension.toLowerCase();
899-
for (int i = 0; i < contentTypes.length; i++) {
900-
if (contentTypes[i][0].equals(extension.trim()))
901-
return contentTypes[i][1];
902-
}
903-
}
904-
return null;
885+
return CommonUtil.getContentFromExt(extension);
905886
}
906887

907888
int GX_NULL_TIMEZONEOFFSET = 9999;
@@ -913,42 +894,6 @@ public void setRestService()
913894

914895
public boolean isRestService()
915896
{ return restService; }
916-
917-
private static final String contentTypes[][] = {
918-
{"txt" , "text/plain"},
919-
{"rtx" , "text/richtext"},
920-
{"htm" , "text/html"},
921-
{"html" , "text/html"},
922-
{"xml" , "application/xml"},
923-
{"aif" , "audio/x-aiff"},
924-
{"au" , "audio/basic"},
925-
{"wav" , "audio/wav"},
926-
{"bmp" , "image/bmp"},
927-
{"gif" , "image/gif"},
928-
{"jpe" , "image/jpeg"},
929-
{"jpeg" , "image/jpeg"},
930-
{"jpg" , "image/jpeg"},
931-
{"jfif" , "image/pjpeg"},
932-
{"tif" , "image/tiff"},
933-
{"tiff" , "image/tiff"},
934-
{"png" , "image/x-png"},
935-
{"3gp" , "video/3gpp"},
936-
{"3g2" , "video/3gpp2"},
937-
{"mpg" , "video/mpeg"},
938-
{"mpeg" , "video/mpeg"},
939-
{"mov" , "video/quicktime"},
940-
{"qt" , "video/quicktime"},
941-
{"avi" , "video/x-msvideo"},
942-
{"exe" , "application/octet-stream"},
943-
{"dll" , "application/x-msdownload"},
944-
{"ps" , "application/postscript"},
945-
{"pdf" , "application/pdf"},
946-
{"svg" , "image/svg+xml"},
947-
{"tgz" , "application/x-compressed"},
948-
{"zip" , "application/x-zip-compressed"},
949-
{"gz" , "application/x-gzip"},
950-
{"json" , "application/json"}
951-
};
952897

953898
public boolean willRedirect()
954899
{

0 commit comments

Comments
 (0)