iT邦幫忙

0

C# WEB端要如何將檔案MAIL出去

  • 分享至 

  • xImage

您好:
C# ,WEB端 xx.cs,是否能夠 讀取SERVER端的資料夾內檔案
並隨MAIL 附件出去呢?

謝謝!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
japhenchen
iT邦超人 1 級 ‧ 2021-08-05 08:06:38
最佳解答

1、先準備一個支援SMTP的郵箱帳號,或用GMail並把SMTP功能打開(在右上角設定裡)

static void sendmail(string who)
{            
    System.Web.Mail.SmtpMail asmtpmail = new System.Web.Mail.SmtpMail();
    using (SmtpClient smtpClient = new SmtpClient())
    {
        var basicCredential = new NetworkCredential("帳號@abc.com", "密碼");
        using (MailMessage message = new MailMessage())
        {
            MailAddress fromAddress = new MailAddress("帳號@abc.com","你的郵箱要用的名字",Encoding.GetEncoding("utf-8")); //寄件人
            smtpClient.Host = "smtp.abc.com";
            smtpClient.UseDefaultCredentials = false; //不使用預設登錄方式
            smtpClient.Credentials = basicCredential; // 上上上上上一行的帳密
            message.SubjectEncoding = Encoding.GetEncoding("utf-8");//都用utf8編碼
            message.BodyEncoding  = Encoding.GetEncoding("utf-8");
            message.From = fromAddress; // 上上上上上一行的寄件者
            message.Subject = "這是主旨,這是主旨,這是主旨";
            message.SubjectEncoding = Encoding.GetEncoding("utf-8"); // 一樣設定主旨也用utf8編碼
            message.IsBodyHtml = true ; // 信件內容用HTML(也可以用TEXT)
            message.Body = $"<p>(如無法使用手機簡訊,可列印本畫面代替)</p><img src='qr.png' />"; 
            message.BodyEncoding = Encoding.GetEncoding("utf-8"); // body也用utf8編碼
            string attimg = Environment.CurrentDirectory + @"\qr.png"; //加入圖片附件

            string g = $"http://abc.com/event/"; // 內容我要加入連結...你自己變
            var pic = generateQR(g);  // 附件圖片是我自己寫的QR扣
            pic.Save(attimg);
            
            // 以下是把圖片加入附件的做法
            Stream astream = Stream.Null;                
            System.Net.Mail.Attachment att1 = new Attachment(attimg);
            att1.Name = System.IO.Path.GetFileName(attimg);
            att1.NameEncoding = Encoding.GetEncoding("utf-8");
            att1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
            att1.ContentDisposition.Inline = true;
            att1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
            message.Attachments.Add(att1);
            // 好囉嗦的做法,但 it's working

            //加入To收件者(可add多人)
            //如用BCC、CC就message.CC.Add(可以再無惱一點嗎)
            message.To.Add(new MailAddress(who,"",Encoding.GetEncoding("utf-8")));

            smtpClient.Send(message); // 寄出
        }
    }
}

看更多先前的回應...收起先前的回應...
noway iT邦研究生 3 級 ‧ 2021-08-05 13:58:45 檢舉

您好:System.Web.Mail.SmtpMail asmtpmail = new System.Web.Mail.SmtpMail();
這一段,就出現如下錯誤

https://ithelp.ithome.com.tw/upload/images/20210805/20104095IzC2c6zOh2.png

過時........你們的主機上跑的是 net.core ?

慘,我沒留意到smtpclient已過時
https://docs.microsoft.com/zh-tw/dotnet/api/system.net.mail.smtpclient?view=net-5.0
我再找其他方法

你改寫一下

using (SmtpClient smtpClient = new SmtpClient("smtp.abc.com",25))
noway iT邦研究生 3 級 ‧ 2021-08-11 14:41:58 檢舉

您好:請問generateQR ,的程式碼? 謝謝!

第一步,到nuget下載安裝Gma.QRCodeNET
第二步,引入GmaQrCodeNet相關命名空間

using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System;
using System.Drawing;
using System.Drawing.Imaging;

第三步,把這個函數放上去

    private Bitmap generateQR(string codeString)
    {
        /*
          Level L (Low)      7%  QRCODE的容錯率
          Level M (Medium)   15% 
          Level Q (Quartile) 25% 
          Level H (High)     30% 
        */
        QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.Q);

        //編碼
        QrCode code = encoder.Encode(codeString);

        //定義線條寬度
        int moduleSizeInPixels = 8;

        //繪二維條碼圖初始化
        GraphicsRenderer renderer = new GraphicsRenderer(
            new FixedModuleSize(moduleSizeInPixels, QuietZoneModules.Two), Brushes.Black, Brushes.White);

        //留白區大小
        int paddinggap = moduleSizeInPixels;
        Point padding = new Point(paddinggap, paddinggap);

        //取得條碼圖大小
        DrawingSize dSize = renderer.SizeCalculator.GetSize(code.Matrix.Width);
        //左右上下留白用
        int imgWidth = dSize.CodeWidth + 2 * padding.X;
        int imgHeight = dSize.CodeWidth + 2 * padding.Y ;
        
        //設定新影像
        Bitmap img = new Bitmap(imgWidth, imgHeight);
                       
        Graphics g = Graphics.FromImage(img);
        //畫上二維條碼圖
        renderer.Draw(g, code.Matrix, padding);
        return img;
    }   

最後一步: 把你要的字串轉成QRCODE並進行處理,如存成jpg檔

var myQR = generateQR("你好嗎?我很好");
myQR.Save("qrcode.jpg");

(我的分數啊啊啊啊.....)

noway iT邦研究生 3 級 ‧ 2021-08-11 17:22:20 檢舉

喔,這式自動產生QRCODE的程式碼,我還以為是讀取檔案的, 謝謝您!

0
小魚
iT邦大師 1 級 ‧ 2021-08-04 20:55:16

如果是在網站資料夾底下的檔案是可以的,
不在資料夾底下的檔案沒有試過,
可以Google找一下資料.

1
jack8900
iT邦新手 2 級 ‧ 2021-08-05 10:06:07
  1. C# ,WEB端 xx.cs,是否能夠讀取SERVER端的資料夾內檔案
    ANS:可以讀取,非網站資料夾底下的需要開權限就可

  2. 隨MAIL 附件出去呢?
    ANS:可以,可以抓取檔案讓他變成EMAIL的附件一起寄送出去

我要發表回答

立即登入回答