Hi arie.keren,
Check this example. Now please take its reference and correct your code.
Refering the below article i have created the example.
HTML
<label>
Upload KTP <span class="required">*</span></label><br />
<asp:FileUpload ID="fuAttachment" runat="server" TabIndex="1" /><br />
<hr />
<asp:Button ID="btnUpload" Text="Upload" runat="server" TabIndex="2" OnClick="btnUpload_Click" />
Namespaces
C#
using System.Configuration;
using System.Drawing.Imaging;
using System.IO;
using System.Net.Mail;
VB.Net
Imports System.IO
Imports System.Net.Mail
Imports System.Drawing.Imaging
Code
C#
protected void btnUpload_Click(object sender, System.EventArgs e)
{
string isiEmail;
string message;
isiEmail = "Test Email With Attach File";
using (MailMessage mm = new MailMessage("kreditnusantara@nusantara-sakti.com", "arie.gunawan@nusantara-sakti.com"))
{
mm.Subject = "Test Email With Attach File";
mm.Body = isiEmail;
if (fuAttachment.HasFile)
{
string filenameKTP = Path.GetFileName(fuAttachment.PostedFile.FileName);
Stream strmKTP = fuAttachment.PostedFile.InputStream;
string sourcePath = fuAttachment.PostedFile.FileName;
var targetFileKTP = Server.MapPath("~/Images/" + filenameKTP);
ReduceImageSize(0.5, sourcePath, targetFileKTP);
mm.Attachments.Add(new Attachment(targetFileKTP));
}
else
{
message = "alert('Foto KTP belum diupload !')";
ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "alert", message, true);
return;
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["Host"];
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
smtp.Send(mm);
}
}
private void ReduceImageSize(double scaleFactor, string sourcePath, string targetPath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath);
var newWidth = System.Convert.ToInt32((image.Width * scaleFactor));
var newHeight = System.Convert.ToInt32((image.Height * scaleFactor));
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
{
thumbnail.Save(targetPath);
}
}
public bool ThumbnailCallback()
{
return false;
}
VB.Net
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim isiEmail As String
Dim message As String
isiEmail = "Test Email With Attach File"
Using mm As MailMessage = New MailMessage("kreditnusantara@nusantara-sakti.com", "arie.gunawan@nusantara-sakti.com")
mm.Subject = "Test Email With Attach File"
mm.Body = isiEmail
If fuAttachment.HasFile Then
Dim filenameKTP As String = Path.GetFileName(fuAttachment.PostedFile.FileName)
Dim strmKTP As Stream = fuAttachment.PostedFile.InputStream
Dim sourcePath As String = fuAttachment.PostedFile.FileName
Dim targetFileKTP = Server.MapPath("~/Images/" & filenameKTP)
ReduceImageSize(0.5, sourcePath, targetFileKTP)
mm.Attachments.Add(New Attachment(targetFileKTP))
Else
message = "alert('Foto KTP belum diupload !')"
ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.[GetType](), "alert", message, True)
Return
End If
mm.IsBodyHtml = False
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = ConfigurationManager.AppSettings("Host")
smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings("EnableSsl"))
Dim NetworkCred As System.Net.NetworkCredential = New System.Net.NetworkCredential()
NetworkCred.UserName = ConfigurationManager.AppSettings("UserName")
NetworkCred.Password = ConfigurationManager.AppSettings("Password")
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = Integer.Parse(ConfigurationManager.AppSettings("Port"))
smtp.Send(mm)
End Using
End Sub
Private Sub ReduceImageSize(ByVal scaleFactor As Double, ByVal sourcePath As String, ByVal targetPath As String)
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(sourcePath)
Dim newWidth = System.Convert.ToInt32((image.Width * scaleFactor))
Dim newHeight = System.Convert.ToInt32((image.Height * scaleFactor))
Using thumbnail As System.Drawing.Image = image.GetThumbnailImage(newWidth, newHeight, New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
thumbnail.Save(targetPath)
End Using
End Sub
Public Function ThumbnailCallback() As Boolean
Return False
End Function