Skip to content

Commit 7394f7d

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 0c632b8 commit 7394f7d

File tree

3 files changed

+85
-61
lines changed

3 files changed

+85
-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
@@ -3521,4 +3521,68 @@ else if ((c >= 'A' && c <= 'Z') ||
35213521
}
35223522
return sb.toString();
35233523
}
3524+
3525+
public static boolean isKnownContentType(String type)
3526+
{
3527+
if (type != null)
3528+
{
3529+
for (int i = 0; i < contentTypes.length; i++)
3530+
{
3531+
if (contentTypes[i].length >= 2)
3532+
{
3533+
if (type.equalsIgnoreCase(contentTypes[i][1]))
3534+
return true;
3535+
}
3536+
}
3537+
}
3538+
return false;
3539+
}
3540+
3541+
public static String getContentFromExt( String extension)
3542+
{
3543+
if (extension != null)
3544+
{
3545+
extension = extension.toLowerCase();
3546+
for (int i = 0; i < contentTypes.length; i++) {
3547+
if (contentTypes[i][0].equals(extension.trim()))
3548+
return contentTypes[i][1];
3549+
}
3550+
}
3551+
return null;
3552+
}
3553+
3554+
private static final String contentTypes[][] = {
3555+
{"txt" , "text/plain"},
3556+
{"rtx" , "text/richtext"},
3557+
{"htm" , "text/html"},
3558+
{"html" , "text/html"},
3559+
{"xml" , "text/xml"},
3560+
{"aif" , "audio/x-aiff"},
3561+
{"au" , "audio/basic"},
3562+
{"wav" , "audio/wav"},
3563+
{"bmp" , "image/bmp"},
3564+
{"gif" , "image/gif"},
3565+
{"jpe" , "image/jpeg"},
3566+
{"jpeg" , "image/jpeg"},
3567+
{"jpg" , "image/jpeg"},
3568+
{"jfif" , "image/pjpeg"},
3569+
{"tif" , "image/tiff"},
3570+
{"tiff" , "image/tiff"},
3571+
{"png" , "image/x-png"},
3572+
{"3gp" , "video/3gpp"},
3573+
{"3g2" , "video/3gpp2"},
3574+
{"mpg" , "video/mpeg"},
3575+
{"mpeg" , "video/mpeg"},
3576+
{"mov" , "video/quicktime"},
3577+
{"qt" , "video/quicktime"},
3578+
{"avi" , "video/x-msvideo"},
3579+
{"exe" , "application/octet-stream"},
3580+
{"dll" , "application/x-msdownload"},
3581+
{"ps" , "application/postscript"},
3582+
{"pdf" , "application/pdf"},
3583+
{"svg" , "image/svg+xml"},
3584+
{"tgz" , "application/x-compressed"},
3585+
{"zip" , "application/x-zip-compressed"},
3586+
{"gz" , "application/x-gzip"}
3587+
};
35243588
}

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)